[英]jQuery selector for data attribute array
Theres' this html code:有这个 html 代码:
<div class="wcpa_form_outer" data-attrrelated="["wcpa-select-1658734650073"]">
for which i'm trying to append html to it.为此,我正在尝试 append html。 I have tried various approaches but none have worked.
我尝试了各种方法,但都没有奏效。
jQuery('.wcpa_form_outer[data-attrrelated="["wcpa-select-1658734650073"]"]').append('some html here');
or或者
jQuery('.wcpa_form_outer[data-attrrelated="[wcpa-select-1658734650073]"]').append('some html here');
or或者
jQuery('.wcpa_form_outer').data('attrrelated').append('some html here');
any clues?任何线索?
The "
"
and/or []
in the attribute value may be the problem Remove it, or try using a part (the most relevant part?) of the attribute value:和/或属性值中的
[]
可能是问题删除它,或尝试使用属性值的一部分(最相关的部分?):
$('[data-attrrelated*="1658734650073"]').append('some html here;'). $('[data-attrrelated*="wcpa-select-165873465007"');append('<br>some html here too!');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wcpa_form_outer" data-attrrelated="["wcpa-select-1658734650073"]"></div>
Problem is that you're using the HTML Entity "
问题是您正在使用 HTML 实体
"
in your attribute.在你的属性中。 This is being translated to a literal quote.
这被翻译成字面引述。 JQuery does not do Entity translation, so it's literally looking for the string
["wcpa-select-1658734650073"]
with ampersands and all, not ["wcpa-select-1658734650073"]
which is the actual value in your attribute. JQuery 不进行实体翻译,因此它实际上是在寻找字符串
["wcpa-select-1658734650073"]
和符号,而不是["wcpa-select-1658734650073"]
,这是您属性中的实际值。
You can work around this by using one of the following methods (after also translating the Entity into a quote in your code).您可以使用以下方法之一解决此问题(在将实体转换为代码中的引号之后)。
attr*=value
) (demonstrated by KooiInc's answer) orattr*=value
)使用 CSS “包含”选择器(由 KooiInc 的回答证明)或attr=value
), shown belowattr=value
),如下所示'["' + value + '"]'
)'["' + value + '"]'
)decodeEntities
function from this answer to translate your attribute value before attempting the lookup (untested, and it's 10 years old)decodeEntities
function 在尝试查找之前转换您的属性值(未经测试,已有 10 年历史) jQuery(`.wcpa_form_outer[data-attrrelated='["wcpa-select-1658734650073"]']`).append('foo')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wcpa_form_outer" data-attrrelated="["wcpa-select-1658734650073"]">append here: </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.