[英]Getting a value/relative url from an anchor tag in JQuery?
I have some HTML like this: 我有一些这样的HTML:
<span class="fetchSeries"><a href="sma10">10</a></span>
I want to get the value "sma10," but the href attribute becomes an absolute path when I try to reference it. 我想获取值“ sma10”,但是当我尝试引用它时,href属性成为绝对路径。 Is there a way to get the relative path of the anchor tag?
有没有办法获取定位标记的相对路径?
Here's what I'm trying to accomplish: 这是我要完成的工作:
If the unique value isn't really a href, you might use data attributes: 如果唯一值实际上不是href,则可以使用数据属性:
HTML 的HTML
<span class="fetchSeries"><a href="#" data-myvalue="sma10">10</a></span>
JavaSCript JavaSCript
$('.fetchSeries a').click(function() {
var myval = $(this).data('myvalue');
});
Docs: http://api.jquery.com/data 文件: http : //api.jquery.com/data
Please refer my answer this stackoverflow post: Jquery how to trigger click event on href element 请参考我的答案这个stackoverflow帖子: jQuery如何触发href元素上的click事件
Or using pure javascript 或使用纯JavaScript
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
from https://gist.github.com/ldong/9735bcd2d3eca8c1038b 来自https://gist.github.com/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. 您可以使用getAttribute来获取href的相对值,只要href的值是相对的(如示例中所示)即可。
ie document.getElementByID("theLink").getAttribute("href");
即
document.getElementByID("theLink").getAttribute("href");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.