简体   繁体   English

jQuery - 选择具有特定html的元素

[英]jQuery - Select element with specific html

I try to select a div which has a specific html. 我尝试选择一个具有特定html的div。 Look at my example: 看看我的例子:

 $("#clickMe").click(function(){ $("div:contains('heinrich')").css("background-color", "limegreen") }); 
 .normal { width: 100px; height: 100px; display: inline-block; } .red { background-color: red; border: 1px solid black; } .blue { background-color: blue; border: 1px solid black; } .yellow { background-color: yellow; border: 1px solid black; } #masterdiv { border: 10px solid gray; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="masterdiv"> My favorite frends are: <div class="red normal" id="div1"> hans </div> <div class="blue normal" id="div2"> franz </div> <div class="yellow normal" id="div3"> heinrich </div> </div> <button id="clickMe"> Clicking me should make only heinrichs div limegreen </button> 

jsFiddle: https://jsfiddle.net/fj1brnv8/2/ jsFiddle: https ://jsfiddle.net/fj1brnv8/2/

However, the parent div's color also changes. 但是,父div的颜色也会发生变化。 Is there a way to only select the element itself, I am not allowed to use ID's. 有没有办法只选择元素本身,我不允许使用ID。

Better mention the className in the selector $("div .normal:contains('heinrich')") 更好地提及选择器$("div .normal:contains('heinrich')")className

 $("#clickMe").click(function(){ $("div .normal:contains('heinrich')").css("background-color", "limegreen") }); 
 .normal { width: 100px; height: 100px; display: inline-block; } .red { background-color: red; border: 1px solid black; } .blue { background-color: blue; border: 1px solid black; } .yellow { background-color: yellow; border: 1px solid black; } #masterdiv { border: 10px solid gray; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="masterdiv"> My favorite frends are: <div class="red normal" id="div1"> hans </div> <div class="blue normal" id="div2"> franz </div> <div class="yellow normal" id="div3"> heinrich </div> </div> <button id="clickMe"> Clicking me should make only heinrichs div limegreen </button> 

Just change the root selector. 只需更改根选择器即可。

UPDATE UPDATE

Select every div and use the filter method . 选择每个div并使用过滤方法

Clone the div you're filtering, select all the children (nested divs), remove them then "come back" to the parent cloned div and retrieve the text. 克隆您正在过滤的div ,选择所有子项(嵌套div),删除它们然后“返回”到父cloned div并检索文本。

Having the text, compare the contents with the text you're searching. 有了文本,将内容与您正在搜索的文本进行比较。

 $("#clickMe").click(function(){ $("div").filter(function(idx, val) { return /heinrich/gi.test($(this).clone().children().remove().end().text()); }).css("background-color", "limegreen") }); 
 .normal { width: 100px; height: 100px; display: inline-block; } .red { background-color: red; border: 1px solid black; } .blue { background-color: blue; border: 1px solid black; } .yellow { background-color: yellow; border: 1px solid black; } #masterdiv { border: 10px solid gray; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="masterdiv"> My favorite frends are: <div class="red normal" id="div1"> hans </div> <div class="blue normal" id="div2"> franz </div> <div class="yellow normal" id="div3"> heinrich </div> </div> <button id="clickMe"> Clicking me should make only heinrichs div limegreen </button> 

在你的exable应该是不同的选择器:

$("#masterdiv > div:contains('heinrich')")

This should do 这应该做

    $("#clickMe").click(function(){ 
$("#masterdiv  div:contains('heinrich')").css("background-color", "limegreen")
 });

Since you didn't want to use ids I can suggest you to try this. 既然你不想使用id我可以建议你试试这个。

   $('div > div:contains(heinrich)').css("background-color", "limegreen")

Working Fiddle: https://jsfiddle.net/g50cfqzw/ 工作小提琴: https//jsfiddle.net/g50cfqzw/

Try this 尝试这个

$("#clickMe").click(function(){
        var html_trg="heinrich";
    $('.normal').each(function(i,u){
            var divtxt=$(u).html();
        divtxt=$.trim(divtxt);
            if(divtxt==html_trg){
          $(u).css("background-color", "limegreen");
        }
    });
});

try this it will work 试试这个会起作用

  $("#clickMe").click(function(){
        $("div#masterdiv").find('div.normal:contains(heinrich)').css("background-color", "limegreen")
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM