Javascript 我是否使用正确的函数使元素变为红色?
好的,我试着让我的标题(全部3个标题)在使用changeStuff函数点击“红色标题”按钮时变成红色,但它似乎运行不正常。这是我应该使用的正确函数吗Javascript 我是否使用正确的函数使元素变为红色?,javascript,jquery,css,Javascript,Jquery,Css,好的,我试着让我的标题(全部3个标题)在使用changeStuff函数点击“红色标题”按钮时变成红色,但它似乎运行不正常。这是我应该使用的正确函数吗 <title>Stanford Graduation</title> <style type="text/css"> .redElements { color: red; } </style> </head> <body> <h1>Grad
<title>Stanford Graduation</title>
<style type="text/css">
.redElements {
color: red;
}
</style>
</head>
<body>
<h1>Graduation</h1>
<p>Graduation is the culmination of four (or more years of hard work).</p>
<h2>Graduation Traditions</h2>
<p>Stanford Graduation has it's own amazing traditions.</p>
<h3>The Wacky Talk</h3>
<p>Stanford Seniors act and dress wacky as they enter the stadium.</p>
<h3 id="speakerHeading">Speakers</h3>
<p>Stanford graduation speakers always have Stanford ties.</p>
<div>
<input type="button" value="Red Headings" id="theRedButton" />
<input type="button" value="Fade Out Speaker" id="theSpeakersButton" />
</div>
<script>
"jquery-2.1.4.js"
</script>
<script>
function changeStuff() {
$("h").addClass("redElements");
}
$("theRedButton").bind("click", changeStuff);
</script>
</body>
斯坦福大学毕业
.再元素{
颜色:红色;
}
毕业典礼
毕业是四年(或更多年)努力工作的顶峰
毕业传统
斯坦福大学的毕业典礼有着自己令人惊叹的传统
古怪的谈话
斯坦福大学的高年级学生在进入体育场时举止古怪
演讲者
斯坦福大学毕业演讲人总是和斯坦福大学有联系
“jquery-2.1.4.js”
函数changeStuff(){
$(“h”).addClass(“再元素”);
}
$(“theRedButton”).bind(“单击”,changeStuff);
$(“#theRedButton”).bind(“单击”,changeStuff)代码>
为“按id选择”添加了一个#
而$('h')
应该是$('h1,h2,h3,h4,h5')代码>//或任何您想要在页面上获取该类的内容
我还可以看到jquery,我希望您尝试演示一个伪代码,否则您必须包括jquery$(“#theRedButton”).bind(“单击”,changeStuff)代码>
为“按id选择”添加了一个#
而$('h')
应该是$('h1,h2,h3,h4,h5')代码>//或任何您想要在页面上获取该类的内容
我还可以看到jquery,我希望您尝试演示一个伪代码,否则您必须包括jquery
$("h1,h2,h3").addClass("redElements");
没有元素名称h
对于id jquery,请使用#
没有#
jquery将提供的选择器视为元素
像这样加上
$("#theRedButton").bind("click",changeStuff);
像这样试试
$("h1,h2,h3").addClass("redElements");
没有元素名称h
对于id jquery,请使用#
没有#
jquery将提供的选择器视为元素
像这样加上
$("#theRedButton").bind("click",changeStuff);
要将类添加到所有标头元素,请使用:
function changeStuff() {
$("h1, h2, h3").addClass("redElements");
}
$("#theRedButton").on("click", changeStuff);
编辑:以下是您需要的完整HTML内容,它还显示了您应该如何引入jQuery并将其挂接到document ready回调中:
<head>
<title>Stanford Graduation</title>
<style type="text/css">
.redElements {
color: red;
}
</style>
</head>
<body>
<h1>Graduation</h1>
<p>Graduation is the culmination of four (or more years of hard work).</p>
<h2>Graduation Traditions</h2>
<p>Stanford Graduation has it's own amazing traditions.</p>
<h3>The Wacky Talk</h3>
<p>Stanford Seniors act and dress wacky as they enter the stadium.</p>
<h3 id="speakerHeading">Speakers</h3>
<p>Stanford graduation speakers always have Stanford ties.</p>
<div>
<input type="button" value="Red Headings" id="theRedButton" />
<input type="button" value="Fade Out Speaker" id="theSpeakersButton" />
</div>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$(document).ready(function() {
$("#theRedButton").on("click", changeStuff);
});
function changeStuff() {
$("h1, h2, h3").addClass("redElements");
}
</script>
</body>
斯坦福大学毕业
.再元素{
颜色:红色;
}
毕业典礼
毕业是四年(或更多年)努力工作的顶峰
毕业传统
斯坦福大学的毕业典礼有着自己令人惊叹的传统
古怪的谈话
斯坦福大学的高年级学生在进入体育场时举止古怪
演讲者
斯坦福大学毕业演讲人总是和斯坦福大学有联系
$(文档).ready(函数(){
$(“#theRedButton”)。在(“单击”,changeStuff);
});
函数changeStuff(){
$(“h1,h2,h3”).addClass(“重元素”);
}
这里有一支工作笔显示了它的作用:要将类添加到所有标题元素中,请使用:
function changeStuff() {
$("h1, h2, h3").addClass("redElements");
}
$("#theRedButton").on("click", changeStuff);
编辑:以下是您需要的完整HTML内容,它还显示了您应该如何引入jQuery并将其挂接到document ready回调中:
<head>
<title>Stanford Graduation</title>
<style type="text/css">
.redElements {
color: red;
}
</style>
</head>
<body>
<h1>Graduation</h1>
<p>Graduation is the culmination of four (or more years of hard work).</p>
<h2>Graduation Traditions</h2>
<p>Stanford Graduation has it's own amazing traditions.</p>
<h3>The Wacky Talk</h3>
<p>Stanford Seniors act and dress wacky as they enter the stadium.</p>
<h3 id="speakerHeading">Speakers</h3>
<p>Stanford graduation speakers always have Stanford ties.</p>
<div>
<input type="button" value="Red Headings" id="theRedButton" />
<input type="button" value="Fade Out Speaker" id="theSpeakersButton" />
</div>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$(document).ready(function() {
$("#theRedButton").on("click", changeStuff);
});
function changeStuff() {
$("h1, h2, h3").addClass("redElements");
}
</script>
</body>
斯坦福大学毕业
.再元素{
颜色:红色;
}
毕业典礼
毕业是四年(或更多年)努力工作的顶峰
毕业传统
斯坦福大学的毕业典礼有着自己令人惊叹的传统
古怪的谈话
斯坦福大学的高年级学生在进入体育场时举止古怪
演讲者
斯坦福大学毕业演讲人总是和斯坦福大学有联系
$(文档).ready(函数(){
$(“#theRedButton”)。在(“单击”,changeStuff);
});
函数changeStuff(){
$(“h1,h2,h3”).addClass(“重元素”);
}
这是一支工作笔,显示了它的作用:您的代码有语法错误
换成
function changeStuff() {
$("h1,h2,h3").addClass("redElements");
}
$("#theRedButton").bind("click", changeStuff);
也
“jquery-2.1.4.js”
应该是
您的代码有语法错误
换成
function changeStuff() {
$("h1,h2,h3").addClass("redElements");
}
$("#theRedButton").bind("click", changeStuff);
也
“jquery-2.1.4.js”
应该是
$(“h”)
应该是$(“h1”)
和/或$(“h2”)
此外,您没有正确添加jQuery:“jQuery-2.1.4.js”是错误的。试试:
还有这个,$(“#theRedButton”).bind(“单击”,changeStuff)代码>,在引用id和时使用#。当引用类$(“h”)
时,应该是$(“h1”)
和/或$(“h2”)
同样,您没有正确添加jQuery:“jQuery-2.1.4.js”
是错误的。试试:
还有这个,$(“#theRedButton”).bind(“单击”,changeStuff)代码>,在引用id和时使用#。指class@charlietfl是的。第二个补充道part@charlietfl是的。添加了第二部分您不能在一个页面上有多个ID-因此您不想使用
。你想使用类你不能在一个页面上有多个ID,所以你不想使用
。你想使用classes我把标签放在什么地方有关系吗,比如在div里面还是外面,因为我改变了一切,但它仍然不起作用?我添加了你对我的代码的编辑,但它仍然不起作用,这就是为什么我问你,把脚本放在哪里有关系吗?你能输出你所有的代码吗。你检查过控制台中的错误吗?我把标签放在什么地方重要吗,比如在div内部还是外部,因为我改变了一切,但它仍然不工作?我添加了你对我的代码的编辑,但它仍然不工作,这就是为什么我问你,我把脚本放在哪里重要吗?你能输出你所有的代码吗。你检查控制台中的错误了吗?我需要把它放在div中吗?比如>>>$(文档)。准备好(函数changeStuff(){$($h1,h2,h3”)。addClass(“redElements”);}$(“#theRedButton”)。绑定(“单击”,