简体   繁体   English

如何在jQuery中使用数据属性值作为ID选择器?

[英]How to use data attribute value as an ID selector in jQuery?

I need to use an ID as a selector by using a data attribute but I can't get it work. 我需要通过使用data属性将ID用作选择器,但是我无法使它正常工作。

Basically, I have a group of links at the very top. 基本上,我在最顶部有一组链接。 Each link have a unique id. 每个链接都有一个唯一的ID。 I use these links to create a link on the fly. 我使用这些链接来动态创建一个链接。 The link created on the fly has a data attribute set with the id of the one I previously clicked on. 动态创建的链接具有一个数据属性集,该属性设置了我先前单击的ID。 Now, I need to target this link with the data attribute using the Id of the link I clicked at first and show the inner text. 现在,我需要使用我首先单击的链接的ID使用data属性将此链接定位为目标,并显示内部文本。

How can I set a data attribute (of an element created on the fly) as an ID selector? 如何设置(动态创建的元素的)数据属性作为ID选择器?

Here is the following Javascript code. 这是下面的Javascript代码。

$('.group-1 a').on('click', function(){

  var block = 
 '<div class="group-2">' +
     '<a href="#" class="child-item" data-item="'+ $(this).attr('id') +'" >Child Item 100</a>' +
  '</div>'; 


$(block).appendTo( $('.host'));


});

$('body').on('click', '.child-item', function(){

      var getId = $(this).attr('data-item');

      var getInner = $('[id="#' + getid + '"]').text();

      $('.result').text(getInner);

})

The HTML code: HTML代码:

 <div class="group-1">
   <a href="#" id="item-1">Parent Item 100</a>
   <a href="#" id="item-2">Parent Item 200</a>
   <a href="#" id="item-3">Parent Item 300</a>
 </div>

 <hr>

 <div class="host"></div>

 <div class="result"></div>

I've prepared a live view at JSFiddle 我已经在JSFiddle中准备了实时视图

The selector you want is not '[id="#myId"]' , but '[id="myId"]' (there is no # ): 您想要的选择器不是'[id="#myId"]' ,而是'[id="myId"]' (没有# ):

$('[id="' + getId + '"]')

or, since you don't use special chars like : : 或者,因为你不使用特殊字符像:

$('#' + getId)

Fixed JSFiddle here (notice you also had getid which I changed to getId ). 在这里修复了 JSFiddle (注意,您也有getid ,我将其更改为getId )。

Now, it pays to notice in these two cases there are two different selectors: the second is an ID selector , and the first is an attribute equals selector (that happens to be comparing the id attribute). 现在,需要注意的是,在这两种情况下,存在两个不同的选择器:第二个是ID选择器 ,第一个是属性等于选择器 (恰好在比较id属性)。

Important Differences Between the ID and Attribute-Equals selectors: ID属性等于选择器之间的重要区别:

1) Elements with duplicated IDs 1)具有重复ID的元素

When there are elements with duplicated IDs (which is invalid HTML, but still can exist): the #foo selector only fetches the first matched element in the DOM (with the foo id); 如果存在具有重复ID的元素 (这是无效的HTML,但仍然存在): #foo选择器获取DOM中第一个匹配的元素(具有foo ID); and the [id='foo'] selector fetches all of them. 然后[id='foo']选择器将全部获取。

2) Escaping 2)转义

In the ID selector, the id is a CSS identifier, so it must match the CSS escaping rules. 在ID选择器中,id是CSS标识符,因此它必须与CSS转义规则匹配。 In the attribute selector, the attribute value is a string. 在属性选择器中,属性值为字符串。

For example, say your ID has a ":" in it: 例如,假设您的ID中带有":"

$("#foo:bar"); // INVALID! :bar is intepreted as a (invalid) pseudo-class

$("#foo\\:bar"); // Valid! The : is properly escaped
$("[id='foo:bar']"); // Valid! The : is inside a string

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

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