我有一些(无效的)HTML代码无法更改:
<a href="#" id="text1"{some-data}>...</a>
<a href="#" id="text2"{some-other-data}>...</a>
使用jQuery,我选择了两个 anchor 之一:
function someFunction(id) {
$('text'+id)...;
}
现在,我想将文本放在大括号内。因此,对于
id=1
,这意味着some-data
;对于id=2
,这将意味着some-other-data
。我怎样才能做到这一点?
简单起见:在一个元素中只会有一个 curl 的东西。
最佳答案
因此,基本上,您想获取“外部” html,然后搜索花括号之间的内容。
第一部分已经在这里解决了
Get selected element's outer HTML
因此,使用该问题中的externalHTML插件
jQuery.fn.outerHTML = function(s) {
return s
? this.before(s).remove()
: jQuery("<div>").append(this.eq(0).clone()).html();
};
function someFunction(id) {
return $('#text'+id).outerHTML().match(/{(.*)}/)[1];
}
关于javascript - 使用jQuery在大括号中获取无效的HTML代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15279830/