Extracting numbers from the parent attribute in jQuery -
By clicking "mylink" I want to be replaced with "123", which is removed from the parent tag.
jQuery:
$ (document) .ready (function () { Click $ (".link") (function () {var comid = $ (this) .parents ("div.comment"). Attr ("class") Match (/ comment - ([0-9] + Replacewith (comid);});});
html:
& quot; div class = "link" & gt; mylink < / Div>
You only have one match, so that you want to use the 0th match (zero-based array) In addition, it will return the whole match, so if you want the number you have to remove the comment -
text.
$ (document) .ready (Function () {$ (". Link"). (Function () {var comid = $ (this) .parents ("div.comment") .attr ("class") .mail (/ comment- [ 0-9] + /) [0]. Replace ('comment-', ''); $ (".link"). Replacewith (comid); }); });
If there is no likelihood that there will be no match, then you want to assign the matches to a variable and if there is a match then only the variable is there - no. ).
Comments
Post a Comment