开发者

jQuery: select text of an element with a particular attribute value

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>
    开发者_StackOverflow社区<div style="position: absolute; width:33px; height:22px; left: 106px; top: 91px; font-size: 21px; color: transparent;">200</div>
</div>


$( ".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);


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜