admin管理员组文章数量:1415683
I have some HTML like this:
<span class="fetchSeries"><a href="sma10">10</a></span>
I want to get the value "sma10," but the href attribute bees an absolute path when I try to reference it. Is there a way to get the relative path of the anchor tag?
Here's what I'm trying to acplish:
- Bind a custom function to click() for any hyperlink within a span class="fetchSeries" tag.
- Extract a value that is unique for that link (eg. sma10).
I have some HTML like this:
<span class="fetchSeries"><a href="sma10">10</a></span>
I want to get the value "sma10," but the href attribute bees an absolute path when I try to reference it. Is there a way to get the relative path of the anchor tag?
Here's what I'm trying to acplish:
- Bind a custom function to click() for any hyperlink within a span class="fetchSeries" tag.
- Extract a value that is unique for that link (eg. sma10).
4 Answers
Reset to default 3If the unique value isn't really a href, you might use data attributes:
HTML
<span class="fetchSeries"><a href="#" data-myvalue="sma10">10</a></span>
JavaSCript
$('.fetchSeries a').click(function() {
var myval = $(this).data('myvalue');
});
Docs: http://api.jquery./data
Use thic code:
$(function(){
var v = $('.fetchSeries a').attr('href');
alert(v);
});
Please refer my answer this stackoverflow post: Jquery how to trigger click event on href element
Or using pure javascript
var parser = document.createElement('a');
parser.href = "http://example.:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example."
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.:3000"
from https://gist.github./ldong/9735bcd2d3eca8c1038b
You could use getAttribute to get the relative value of the href as long as the value of the href is relative like in the example.
i.e.
document.getElementByID("theLink").getAttribute("href");
本文标签: javascriptGetting a valuerelative url from an anchor tag in JQueryStack Overflow
版权声明:本文标题:javascript - Getting a valuerelative url from an anchor tag in JQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745239218a2649222.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论