设置 href 属性会替换链接的文本
编辑:我不是在谈论状态栏显示,而是在谈论您单击以跟踪链接的文本。 /编辑
我浏览了一些类似的帖子,但没有找到答案。我有一个想要显示的链接。
www.mylink.com
我希望每次单击链接时链接的 href 都会更改。
href="www.mylink.com?R=340978"
每次单击后,查询字符串都会更改为随机数,以防止浏览器缓存页面。预计该页面将频繁更改并经常被查看以评估这些更改。
所以我希望人们看到“www.mylink.com”,但链接到“www.mylink.com?R=34532”我想我可以通过一个 onclick 事件来实现这一点,该事件会附加一个新的随机数并设置href 属性添加到新链接,但这也会更改显示的文本。
<script type="text/javascript">
var baseLink = "http://myDevLink/library/ea26098f-89d1-4305-9f75-afc7937f8336/25/test/036b602b-0bde-e011-957b-1cc1dee8bacd.html";
var link = "";
// Prime the link
$(document).ready(function()
{
NewRandomParam();
});
function NewRandomParam()
{
// Make sure one of the warning messages isn't displayed instead of the link
if (document.getElementById("link") != null)
{
// Add a random number param to the query string to prevent the user from opening a cached version of the page
link = baseLink + "?R=" + Math.floor(Math.random() * 1000000);
document.getElementById("link").setAttribute("href", link);
}
}
</script>
<body>
<div id="divContent" style="display:inline; border:0px" class="tab">
<pre style="margin-top: 0;" >
<font face="tahoma">
<a href="" id="link" onclick=NewRandomParam(); target="_blank">http://myDevLink/library/ea26098f-89d1-4305-9f75-afc7937f8336/25/test/036b602b-0bde-e011-957b-1cc1dee8bacd.html</a>
</font>
</pre>
</div>
</body>
</html>
正如您从上面的代码中看到的,没有关于应该显示什么的查询字符串,但是当我查看页面时,href 的值与查询字符串一起显示。如果我查看源代码,正确的网址就在那里并且应该显示,但事实并非如此。
请大家帮我看看我哪里愚昧了。
EDIT: I am not talking about the status bar display, I'm talking about the text you click on to follow the link. /EDIT
I looked through a few similar posts and didn't find an answer. I have a link that I want displayed.
www.mylink.com
I want the href of the link to change every time the link is clicked.
href="www.mylink.com?R=340978"
The query string changes to a random number after each click to prevent the browser from caching the page. It's expected that the page will be changing frequently and be viewed frequently to assess those changes.
So I want people to see "www.mylink.com" but the link to lead to "www.mylink.com?R=34532" I thought I would achieve this with an onclick event that would apend a new random number and set the href attribute to the new link, however this is also changing the text that is displayed.
<script type="text/javascript">
var baseLink = "http://myDevLink/library/ea26098f-89d1-4305-9f75-afc7937f8336/25/test/036b602b-0bde-e011-957b-1cc1dee8bacd.html";
var link = "";
// Prime the link
$(document).ready(function()
{
NewRandomParam();
});
function NewRandomParam()
{
// Make sure one of the warning messages isn't displayed instead of the link
if (document.getElementById("link") != null)
{
// Add a random number param to the query string to prevent the user from opening a cached version of the page
link = baseLink + "?R=" + Math.floor(Math.random() * 1000000);
document.getElementById("link").setAttribute("href", link);
}
}
</script>
<body>
<div id="divContent" style="display:inline; border:0px" class="tab">
<pre style="margin-top: 0;" >
<font face="tahoma">
<a href="" id="link" onclick=NewRandomParam(); target="_blank">http://myDevLink/library/ea26098f-89d1-4305-9f75-afc7937f8336/25/test/036b602b-0bde-e011-957b-1cc1dee8bacd.html</a>
</font>
</pre>
</div>
</body>
</html>
As you can see from the above code, there is no query string on what should be displayed however when I view the page the value of the href is displayed with the query string. If I do a view source the correct url is there and should be displayed but it isn't.
Please enlighten me on where I'm being dumb.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案是这里建议的内容的组合,加上必须将链接文本重置为我想要的内容。这是有效的。
谢谢大家。
The solution was a combination of things suggested here plus having to reset the links text to what I wanted it. Here is what worked.
Thanks to all of you.
随机数在这里不是一个好主意。总是有机会获得重复的号码。只需使用
date.getTime()
即可,它应该足够“随机”以满足您的目的。更改 href 不应更改链接中的文本。它们有时是相同的文本,但这是通过将相同的信息放在两个位置(在 href 属性内)并作为
linkname 标记本身内的文本来实现的。更改一个永远不会影响另一个,除非您有其他一些代码可以保持同步。
如果您的意思是要更改链接悬停时窗口状态栏中显示的内容,祝您好运。大多数浏览器都有机制来防止这种摆弄,因为更改状态栏文本被垃圾邮件发送者和诈骗者严重滥用。
Random numbers aren't a good idea here. There's always the chance of getting a duplicated number. Just use
date.getTime()
which should be "random" enough for your purposes.Changing the href should NOT be changing the text in the link. They're sometimes the same text, but that's by putting the same information in two places (inside the href attribute) and as text inside the
<a>linkname</a>
tag itself. Changing one should never affect the other, unless you've got some other code that keeps things synced.If you mean you want the change what's displayed in the window status bar when the link's hovered, good luck. most browsers have mechanisms to prevent such fiddling in the first place, as changing the status bar text was heavily abused by spammers and scammers.