Javascript Jquery没有选择全部
可能重复: 我希望jquery选择id=box的所有div:Javascript Jquery没有选择全部,javascript,jquery,html,select,Javascript,Jquery,Html,Select,可能重复: 我希望jquery选择id=box的所有div: <!DOCTYPE html> <head> <link rel="stylesheet" href="css/main.css" type="text/css"/> <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script> <script src="js/si
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="css/main.css" type="text/css"/>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="js/sizer.js"></script>
<title>
Design tests
</title>
</head>
<body>
<div id="box" style="width:300px;">
Hallo
</div>
<div id="box" style="width:300px;">
Hallo
</div>
<script>
$(document).ready(function(){
$("#box").mouseenter(function(){ $(this).css("border", "1px gray outset");}).mouseleave(function(){ $(this).css("border", "none");});
});
</script>
</body>
</html>
但它只选择了第一个。谁能帮帮我吗?这并不难,是吗?HTML只允许一个元素具有给定的id。这就是jQuery内部使用返回一个元素的元素的原因 发件人: id=名称[CS] 此属性为元素指定名称。此名称在文档中必须是唯一的 如果要选择多个元素,请使用类,而不是id:
<div class="box" style="width:300px;">
Hallo
</div>
<div class="box" style="width:300px;">
Hallo
</div>
<script>
$(document).ready(function(){
$(".box").mouseenter(function(){ $(this).css("border", "1px gray outset");}).mouseleave(function(){ $(this).css("border", "none");});
});
</script>
同一ID不能超过1个,请改用类
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="css/main.css" type="text/css"/>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="js/sizer.js"></script>
<title>
Design tests
</title>
</head>
<body>
<div class="box" style="width:300px;">
Hallo
</div>
<div class="box" style="width:300px;">
Hallo
</div>
<script>
$(document).ready(function(){
$(".box").mouseenter(function(){ $(this).css("border", "1px gray outset");}).mouseleave(function(){ $(this).css("border", "none");});
});
</script>
</body>
</html>
将选择器更改为divbox,它将像这样工作
$(document).ready(function(){
$("div#box").mouseenter(function(){ $(this).css("border", "1px gray outset");}).mouseleave(function(){ $(this).css("border", "none");});
});
还要注意的是,使用相同id的多个元素是不好的做法。
语法$'box'表示转到并选择id与box匹配的第一个元素。你要做的是改用class,或者如果你坚持使用id$'div[id=box],你会做你想做的事。@atifmohammedamednumuddin不:我已经过了销售上限很久了。是的,我知道。。我希望我能很快到达那里,但这个回答给我带来了我的第17顶帽子:试试看吧,人们。免费重复!为什么会有这么多的反对票?仅仅因为一个问题是基本的并不意味着它是坏的。哈哈,你不能有一个以上的id。改用类名。然后是美元。box@Forty-两个我同意。这不应该被提高投票率,但这么多的反对票。。。这是没有必要的…而且已经有人喜欢了,我能闻到讽刺+1,你是冠军!!