jQuery:选择具有特定属性值的元素的文本
我正在尝试获取下面标记中 div 的文本内容。但是,我似乎无法选择 class='ui-selected' 的 div 的文本内容。我需要“200”、“x”、“200”。我如何获得这些?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.2r1/build/reset/reset-min.css"> <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.2r1/build/reset/reset-min.css">
<style>
#selectable .ui-selecting { background-color:#2288FF; opacity:0.2; }
#selectable .ui-selected { background-color:#2288FF; opacity:0.2; }
</style>
<script>
$(document).ready(function() {
$("#selectable").selectable();
});
$(function() {
$( "#selectable" ).selectable({
stop: function() {
$( ".ui-selected", this ).each(function() {
alert ($("div[class='ui-selected'].text"));
});
}
});
});
</script>
<div id="selectable">
<img src="http://placehold.it/200x200"></img>
<div style="position: absolute; width:33px; height:22px; left: 54px; top: 91px; font-size: 21px; color: transparent;">200</div>
<div style="position: absolute; width:10px; height:22px; left: 92px; top: 91px; font-size: 21px; color: transparent;">x</div>
<div style="position: absolute; width:33px; height:22px; left: 106px; top: 91px; font-size: 21px; color: transparent;">200</div>
</div>
I'm trying to get the textual contents of the divs in the markup below. However, I can't seem to select the textual content of the div with class='ui-selected'. I need to "200", "x", "200". How do I get those?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.2r1/build/reset/reset-min.css"> <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.2r1/build/reset/reset-min.css">
<style>
#selectable .ui-selecting { background-color:#2288FF; opacity:0.2; }
#selectable .ui-selected { background-color:#2288FF; opacity:0.2; }
</style>
<script>
$(document).ready(function() {
$("#selectable").selectable();
});
$(function() {
$( "#selectable" ).selectable({
stop: function() {
$( ".ui-selected", this ).each(function() {
alert ($("div[class='ui-selected'].text"));
});
}
});
});
</script>
<div id="selectable">
<img src="http://placehold.it/200x200"></img>
<div style="position: absolute; width:33px; height:22px; left: 54px; top: 91px; font-size: 21px; color: transparent;">200</div>
<div style="position: absolute; width:10px; height:22px; left: 92px; top: 91px; font-size: 21px; color: transparent;">x</div>
<div style="position: absolute; width:33px; height:22px; left: 106px; top: 91px; font-size: 21px; color: transparent;">200</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是
我刚刚更正了语法,但是您需要考虑
$('.ui-selected')
可以匹配许多项目should be
I just corrected the syntax, but you need to take in consideration that
$('.ui-selected')
can match many items应该是
should be