包含HTML的变量上的jQuery选择器
jQuery selector on variable containing HTML
我正在尝试选择一个html变量中包含的所有锚点,如下所示:我的代码:
var deposti_row = "
<a data-country='uk' data-currency='eur' data-matching='country-currency'> AA </a>
<a data-country='uk' data-currency='eur' data-matching='country'> BB </a>
<a data-country='uk' data-currency='eur' data-matching='none'> CC </a>
"
$("a['data-matching']",$(deposit_row)).each( function(){
console.log(this);
});
但我得到了错误语法错误,无法识别的表达式:a['datamatching']。请帮忙吗?
您的语法错误。无论如何,你真正想要的是过滤匹配集:
$(deposit_row).filter('a[data-matching]').each(...)
在处理多行时,您的字符串语法也是错误的。并且CCD_ 1与CCD_。
发布的代码太少,这让很多事情都错了。。。
这是另一种方法:-
- 使用
'
创建多行字符串 - 由于没有根,请在每个根中使用
has
进行检查
var deposit_row = "'
<a data-country='uk' data-currency='eur' data-matching='country-currency'> AA </a>'
<a data-country='uk' data-currency='eur' data-matching='country'> BB </a>'
<a data-country='uk' data-currency='eur' data-matching='none'> CC </a>'
";
$(deposit_row).each(function() {
if ($(this).has('[data-matching]'))
console.log(this);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
您需要从属性选择器中删除单引号,然后它应该像长锚标记a
具有父元素一样工作。
您需要将它们封装在div
中,以将它们视为后代
deposit_row = $(deposit_row).wrap("<div></div>").html();
$("a[data-matching]",$(deposit_row)).each( function(){
console.log(this);
});
如果a['data-matching']
是根节点的子节点,也可以尝试此操作
$(deposit_row).find("a['data-matching']").each(function(){
});
但由于a
是的根节点
$(deposit_row).children("a['data-matching']").each(function(){
});
相关文章:
- jQuery选择器无法正常工作
- 将jquery选择器转换为数组
- JavaScriptDOM正常工作时JQuery选择器不工作
- 在我的情况下,使用带有变量失败的 jquery 选择器
- 换行符插入jquery选择器
- 使用javascript"这个“;用于jquery选择器
- jQuery选择器缓存问题
- jQuery 选择器,所有跨度都在 DIV 内
- JQuery选择器:如果同级具有.class,则选择td
- jQuery选择器不识别任何动态创建的HTML输入函数
- 循环遍历元素jquery选择器
- jQuery选择器错误:无法识别的表达式
- 自定义jquery选择器属性未在模拟器中激发
- Jquery选择器为空,我应该使用哪个作用域
- jQuery选择器就是用这个构建的
- jQuery 选择器混淆
- 将jQuery选择器与“this”相结合
- jQuery选择器在脚本中不起作用,但在控制台中工作
- jQuery 选择器中的简单 jQuery 语法和串联
- 全局 JQuery 选择器缓存以提高性能