admin管理员组文章数量:1332389
I have a data-attribute with a unique name and a number at the end.
data-target="foo-bar-n"
where n is the unique number.
I want to be able to get that number but it isn't working.
I call:
.data("target")
on the object I want the target from and It returns fine, but when I use a regex search on the results, I get a pletely different number, which I suspect is because returned data is an object with other data. What I need to know is how to get that number out, or how to convert the data-attribute into a string. (I've tried toString, and that doesn't work.)
Here is my code:
var selection= window.getSelection().getRangeAt(0);
var showSelection = $(selectionmonAncestorContainer.parentNode.parentNode)
.data('target');
console.log(showSelection);
console.log(showSelection.search(/\d+/));
The console logs
#ment_for_paragraph_17
23
The program is meant to let a user select something on the page and highlight it.
I have a data-attribute with a unique name and a number at the end.
data-target="foo-bar-n"
where n is the unique number.
I want to be able to get that number but it isn't working.
I call:
.data("target")
on the object I want the target from and It returns fine, but when I use a regex search on the results, I get a pletely different number, which I suspect is because returned data is an object with other data. What I need to know is how to get that number out, or how to convert the data-attribute into a string. (I've tried toString, and that doesn't work.)
Here is my code:
var selection= window.getSelection().getRangeAt(0);
var showSelection = $(selection.monAncestorContainer.parentNode.parentNode)
.data('target');
console.log(showSelection);
console.log(showSelection.search(/\d+/));
The console logs
#ment_for_paragraph_17
23
The program is meant to let a user select something on the page and highlight it.
Share Improve this question edited Sep 27, 2014 at 23:21 King_llama asked Sep 27, 2014 at 23:11 King_llamaKing_llama 1792 gold badges2 silver badges12 bronze badges 2-
1
Can you provide your code? Also, data attributes are still just attributes. Try
.attr('data-target')
to get the string value of that attribute... and this regex should work:/([\d]+)$/
– Ryan Wheale Commented Sep 27, 2014 at 23:13 - I have provided my code. When I try the attr method, I get the same results. – King_llama Commented Sep 27, 2014 at 23:25
3 Answers
Reset to default 2If you are using jQuery 1.4.3 or later you can use
.data("target");
otherwise you must use
.attr("data-target");
But your requirement appears to be extracting a number from a formatted string parameter?
HTML:
<div id="foo" data-target="foo-bar-5"></div>
jQuery:
var num = $('#foo').data('target').split('-')[2];
Or regex:
var num = $('#foo').data('target').match(/\d+/);
Or if your requirement is specifically capture the last number in the string (eg: foo-bar-55-23 would result in '23')
var num = $('#foo').data('target').split('-').slice(-1);
// regex
var num = $('#foo').data('target').match(/\d+$/);
See fiddle
I suspect is because returned data is an object with other data.
That doesn't make any sense at all. The reason you are not getting the number in the string is because .search
returns the index of the match. Use .match
or .exec
instead:
showSelection.match(/\d+/)[0];
/*or*/
/\d+/.exec(showSelection)[0];
Try:
.data('target').split('-').slice(-1)[0];
.split will split your string into an array with each array element being a word
.slice(-1) will return an array consisting of the last element of your array
[0] accesses the array element
本文标签: javascriptJquery datatarget as string possibleStack Overflow
版权声明:本文标题:javascript - Jquery data-target as string possible? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289720a2447567.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论