简体   繁体   中英

Get invalid HTML code in curly brackets with jQuery

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>

With jQuery, I select one of the two anchors:

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

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 .

How can I do this?

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

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

The first part of that has been solved here

Get selected element's outer HTML

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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