使用jQuery在大括号中获取无效的HTML代码 [英] Get invalid HTML code in curly brackets with jQuery

查看:60
本文介绍了使用jQuery在大括号中获取无效的HTML代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些(无效的)HTML代码我无法更改

I have some (invalid) HTML code which I cannot change:

<a href="#" id="text1"{some-data}>...</a>
<a href="#" id="text2"{some-other-data}>...</a>

使用jQuery,我选择两个锚中的一个:

With jQuery, I select one of the two anchors:

function someFunction(id) {
  $('text'+id)...;
}

现在,我想在大括号内找到文本。因此,对于 id = 1 ,这意味着 some-data ,对于 id = 2 这将是 some-other-data

Now, I'd like to get the text inside the curly brackets. So, for id=1 this would mean some-data, for id=2 this would be some-other-data.

我该怎么办?

为了方便起见:在一个元素中只有一个卷曲的东西。

To make it easy: there will be only one curly bracked thing in one element.

推荐答案

所以,基本上你想得到'外部'html,然后在大括号之间搜索什么。

So, basically you want to get the 'outer' html and then search for whats in between the curly braces.

第一部分有已在此解决

获取所选元素的外部HTML

因此,使用该问题的outerHTML插件

So using the outerHTML plugin from that question

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];
}

这篇关于使用jQuery在大括号中获取无效的HTML代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆