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

时间:2010-11-22 22:43:54

标签: jquery jquery-ui jquery-selectors

我正在尝试在下面的标记中获取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>

2 个答案:

答案 0 :(得分:1)

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

应该是

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

答案 1 :(得分:0)

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

应该是

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

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

相关问题