javascript 使用 JS 和 HTML 将当前 URL 插入链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34145408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert current URL into a link using JS and HTML
提问by Darkseven
So, Ive read through similar things but I still can't find an answer that applies more closely to what I'm doing. I am attempting to use JS to get the current page URL and append it to a social media sharing link like this:
所以,我已经阅读了类似的东西,但我仍然找不到更适合我正在做的事情的答案。我正在尝试使用 JS 获取当前页面 URL 并将其附加到社交媒体共享链接,如下所示:
<a href="http://reddit.com/submit?url=CURRENTPAGE.html; title="This is a post!" target="_blank">
Using Javascript, I've managed to assign the current URL to a variable:
使用 Javascript,我设法将当前 URL 分配给一个变量:
<script>
var x = window.location.href;
document.getElementById("smsharing").innerHTML = x;
</script></p>
And I made sure it worked by doing a test display of it. So what exactly is the proper method/syntax for actually putting 'x' in place of CURRENTPAGE.html???
我通过对它进行测试显示来确保它有效。那么实际将“x”代替 CURRENTPAGE.html 的正确方法/语法到底是什么???
I know this is a STUPID question, but I'm really stumped. Specifics help, because part of the problem is that I have precious little knowledge of JS. Thoughts?
我知道这是一个愚蠢的问题,但我真的很难过。具体帮助,因为部分问题是我对JS的了解很少。想法?
采纳答案by Sharanya
This should do it:
这应该这样做:
<script>
baseurl="http://www.facebook.com?"
function buildURL(item)
{
item.href=baseurl+window.location.href;
return true;
}
</script>
</head>
<body>
<a onclick="return buildURL(this)" href="">Google</a>
</body>
回答by Victory
Get the elements current href which doesn't have the url
value and append the current url.
获取没有url
值的元素 current href并附加当前 url。
Modified HTML
修改后的 HTML
<a id='smsharing'
href="http://reddit.com/submit?url="
title="This is a post!"
target="_blank">link</a>
Script
脚本
<script>
var x = window.location.href;
var link = document.getElementById("smsharing"); // store the element
var curHref = link.getAttribute('href'); // get its current href value
link.setAttribute('href', curHref + x);
</script>
回答by Wowsk
Using just pure JavaScript you can set the href of the link by just having the base href as a string and then add the variable where ever it is needed.
仅使用纯 JavaScript,您就可以通过将基本 href 作为字符串来设置链接的 href,然后在需要的地方添加变量。
var x = window.location.href;
document.getElementById("linkid").href = "http://reddit.com/submit?url="+encodeURIComponent(x);
回答by Johannes
Create another variable for the complete href attribute of your link:
为链接的完整 href 属性创建另一个变量:
var myURL = "http://reddit.com/submit?url=" + x;
var myURL = "http://reddit.com/submit?url=" + x;
Then replace the current href attribute with that variable:
然后用该变量替换当前的 href 属性:
docment.getElementById("YourLinkTagID").href = myURL
docment.getElementById("YourLinkTagID").href = myURL
回答by Korgrue
Using jQuery it is as simple as:
使用 jQuery 就像这样简单:
$('a').attr("href",x);
回答by Zakaria Acharki
You should simply replace innerHTML
by href
:
您应该简单地替换innerHTML
为href
:
document.getElementById("smsharing").href = x;
Hope this helps.
希望这可以帮助。
回答by jperezov
Once you have access to the current URL, you then want to find the element and replace CURRENTPAGE.html. To do so, you'll need some way to select the element. Let's give it an ID:
访问当前 URL 后,您需要查找该元素并替换 CURRENTPAGE.html。为此,您需要某种方式来选择元素。让我们给它一个ID:
<a id="myLink" href="http://reddit.com/submit?url=CURRENTPAGE.html"></a>
Now we can grab the link like so:
现在我们可以像这样获取链接:
var link = document.getElement('myLink');
Let's get the URL again, and give it a better variable name:
让我们再次获取 URL,并给它一个更好的变量名:
var url = window.location.href;
Now let's update the HREF attribute of link
:
现在让我们更新 的 HREF 属性link
:
link.href = link.href.replace('CURRENTPAGE.html', url);
And that's it!
就是这样!