jQuery:选择具有特定属性值的元素的文本

发布于 2024-10-03 11:53:47 字数 1884 浏览 1 评论 0原文

我正在尝试获取下面标记中 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我只土不豪 2024-10-10 11:53:48
alert ($("div[class='ui-selected'].text")); 

应该是

alert ($(".ui-selected").text()); 

我刚刚更正了语法,但是您需要考虑 $('.ui-selected') 可以匹配许多项目

alert ($("div[class='ui-selected'].text")); 

should be

alert ($(".ui-selected").text()); 

I just corrected the syntax, but you need to take in consideration that $('.ui-selected') can match many items

一个人的旅程 2024-10-10 11:53:47
$( ".ui-selected", this ).each(function() {
    alert ($("div[class='ui-selected'].text"));
 });

应该是

var textyouwant = '';
$("div.ui-selected").each(function(){
    textyouwant += $(this).html();
});
alert(textyouwant);
$( ".ui-selected", this ).each(function() {
    alert ($("div[class='ui-selected'].text"));
 });

should be

var textyouwant = '';
$("div.ui-selected").each(function(){
    textyouwant += $(this).html();
});
alert(textyouwant);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文