找到标题等于某事的div

时间:2014-10-16 10:38:44

标签: javascript jquery

<div id="subjectissueref"><div id="subjectissue"> 
<div title="myDivTitle" class="cbs-Line1Link  ms-displayBlock"id="ctl00_PlaceHolderMain_g_9913c0d3_0331_4643_8d90_82268670a0f7_csr1_2lines_line1">
<b>Subject :</b> myDivTitle</div> 
<div title="anotherDivTitle" class="cbs-Line1Link  ms-displayBlock"id="ctl00_PlaceHolderMain_g_9913c0d3_0331_4643_8d90_82268670a0f7_csr1_2lines_line6"
><b>Issue :</b> anotherDivTitle</div></div>
 <div title="53" class="cbs-Line1Link  ms-displayBlock" id="ctl00_PlaceHolderMain_g_9913c0d3_0331_4643_8d90_82268670a0f7_csr1_2lines_line7" style="float: right;">
<b>Ref :</b> #53</div></div>

我有这段代码,我想用javascript找到title等于myDivTitle的元素

$('#subjectissureref').find('div[title=myDivTitle]')

4 个答案:

答案 0 :(得分:1)

试试这个:

$("div[title='myDivTitle']") // This will get div element

答案 1 :(得分:0)

试试这个:

elem = $("div[title='myDivTitle']");

elem = $("div").prop('title', 'myDivTitle');

答案 2 :(得分:0)

<强> FIDDLE

$("div").each(function(){
    if($(this).prop('title')=="myDivTitle"){
        alert( "This div has title as myDivTitle");
    }
});

答案 3 :(得分:0)

或者,您可以将jQuery.filter(...)与自定义函数一起使用以获得一些特殊选择,例如:

jQuery(document).ready(function() {
    jQuery('#subjectissueref .cbs-Line1Link').filter(function(index, elem) {
        return jQuery(elem).attr('title') == 'anotherDivTitle';
    }).css('background-color', 'green');
});

但在你的情况下,这是最短的解决方案:

jQuery(document).ready(function() {
    jQuery('#subjectissueref div[title="myDivTitle"]').css('background-color', 'red');
});

在此处试试:http://jsfiddle.net/algorhythm/1o1hjw61/