简体   繁体   English

通过ID获取Div,并且Div的ID包含多个值jquery

[英]Get Div by Id and Id of Div contains multiple values jquery

I have a Div element on my HTML page, and that DIV is coming from an ASP.NET application, so the DIV ID is changing all the time but few words remain the same in that id. 我的HTML页面上有一个Div元素,并且DIV来自ASP.NET应用程序,因此DIV ID一直在变化,但是在该ID中几乎没有单词保持相同。

For example: 例如:

<div id="ctl00_ctl00_MainContent_DetailBody_ctl01_ctl02_ctl00__UWT_ctl01_ctl00_ctl04
_NewGrid"> </div>

The only things which remains same all the time in above example are "_UWT" & "_NewGrid". 在上面的示例中,唯一始终保持不变的是“ _UWT”和“ _NewGrid”。

I know how to get the by Exact ID or atleast by using the 1 word in this: $( "div[id$='_UWT']" ) 我知道如何通过使用其中的1个单词来获取确切ID或至少:$(“ div [id $ ='_ UWT']”)

But I need to get this Div element by using the multiple parameters: I need to check the "_UWT" and "_NewGrid" also. 但是我需要使用多个参数来获取这个Div元素: 我还需要检查“ _UWT”和“ _NewGrid”。 If both words exist in the Div id, then return me the element only. 如果两个单词都存在于Div id中,则仅将元素返回给我。

I need to get this DIV by JQuery. 我需要通过JQuery获得此DIV。 I know I can set the ClientID to Static from ASP.NET, but that is not doable in my case. 我知道我可以从ASP.NET将ClientID设置为Static,但是在我的情况下这是不可行的。 Thanks. 谢谢。

One way could be: 一种方法是:

$('div').each(function() {
if($(this).attr('id').includes('_NewGrid') && $(this).attr('id').includes('_UWT')) {
console.log($('div').attr('id'));
$(this).css('color','red') // do whatever you want with div
}
})

Demo: 演示:

 $('div').each(function() { if($(this).attr('id').includes('_NewGrid') && $(this).attr('id').includes('_UWT')) { console.log($('div').attr('id')); $(this).css('color','red') } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="ctl00_ctl00_MainContent_DetailBody_ctl01_ctl02_ctl00__UWT_ctl01_ctl00_ctl04 _NewGrid">11111</div> <div id="ctl00_ctl00_MainContent_DetailBody_ctl01_ctl02_ctl00__UWT_ctl01_ctl00_ctl04 _NewGri">222222222</div> <div id="ctl00_ctl00_MainContent_DetailBody_ctl01_ctl02_ctl00__ctl01_ctl00_ctl04 _NewGrid">3333333333333</div> 

To achieve this you can combine the 'attribute contains' selector (to find the _UWT ) and the 'attribute ends with' selector (to find the _WebGrid ), like this: 为此,您可以结合使用“属性包含”选择器(以找到_UWT )和“属性以...结尾”选择器(以找到_WebGrid ),如下所示:

 $('div[id*="UWT"][id$="_NewGrid"]').addClass('foo'); 
 .foo { color: #C00; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="ctl00_ctl00_MainContent_DetailBody_ctl01_ctl02_ctl00_ctl01_ctl00_ctl04_NewGrid">Not this one</div> <div id="ctl00_ctl00_MainContent_DetailBody_ctl01_ctl02_ctl00__UWT_ctl01_ctl00_ctl04">Not this one</div> <div id="ctl00_ctl00_MainContent_DetailBody_ctl01_ctl02_ctl00__UWT_ctl01_ctl00_ctl04_NewGrid">This one</div> 

Try following way: 请尝试以下方式:

Add specific class I added here item-collection : 添加我在此处添加的特定类item-collection

<div class="item-collection" id="ctl00_ctl00_MainContent_DetailBody_ctl01_ctl02_ctl00__UWT_ctl01_ctl00_ctl04
_NewGrid">Div 1</div>
<div class="item-collection" id="ctl00_ctl00_MainContent_DetailBody_ctl01_ctl02_ctl00__UWT_ctl01_ctl00_ctl04">Div 2</div>
<div class="item-collection" id="ctl00_ctl00_MainContent_DetailBody_ctl01_ctl02_ctl00_ctl01_ctl00_ctl04
_NewGrid">Div 3</div>
<div class="item-collection" id="ctl00_ctl00_MainContent_DetailBody_ctl01_ctl02_ctl00_ctl01_ctl00_ctl04
_NewGrid">Div 4</div>

JS is: JS是:

    var itemslist = [];
    $(".item-collection").each(function(){
        if($(this).attr("id").indexOf("_UWT") > -1 && $(this).attr("id").indexOf("_NewGrid")){
            itemslist.push($(this))
        }
    })      
    console.log(itemslist);

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

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