简体   繁体   English

jQuery按数据属性选择元素意外结果

[英]jQuery Select Element by Data Attribute Unexpected Results

The following code should alert the HTML content of a DIV. 以下代码应警告DIV的HTML内容。 The DIV is selected by finding its data attribute for the field panel. 通过找到字段面板的数据属性来选择DIV。 However the alert is undefined for reasons I cannot explain. 但是由于我无法解释的原因,警报未定义。

$('.access .panel').html(results).data('panel','logon');

  alert($('.access .panel[data-panel=logon]').html());

Using the data method to set the data for an element doesn't change the data attribute. 使用data方法设置元素的数据不会更改data属性。 The data attribute is the initial value, not the current value. 数据属性是初始值,而不是当前值。

If you set the data attribute, you can find it using the selector: 如果设置了data属性,则可以使用选择器找到它:

$('.access .panel').html(results).attr('data-panel','logon');

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

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