使用 javascript 更改锚标记的 href
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3998496/
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
Change the href of an anchor tag with javascript
提问by Tom
Hey I have a question for javascript. I need to assign an href value to an anchor tag or asp:HyperLink. SOMETHING. that will allow me to link text in a dialog popup to an href that a function specifies. Here is my code.
嘿,我有一个关于 javascript 的问题。我需要为锚标记或 asp:HyperLink 分配一个 href 值。某物。这将允许我将对话框弹出窗口中的文本链接到函数指定的 href。这是我的代码。
<'custom:JQueryDialog I made' runat=server ID="dialogPopUp" AutoOpen="false"
CloseOnEscape="true" Modal="true" Title="Download" width="300px">
//I will spare you all of the div tags for formatting
<a runat="server" id="downloadLink" target="_blank" class="'css with an icon'"
href=""></a>
</'custom:JQueryDialog I made'>
Now I am having to get an fso from the database since that is where the info is stored. This fso is different depending on what the entity reflector class sends to this javascript. I have a function that formats javascript strings similar to C# I found. I then have another function that gets the fso from the entity reflector class. This works. I tested the string by displaying it in an alert and this works fine. The problem I am having is setting the href of the anchor tag with javascript. I am going nuts! Please help!
现在我必须从数据库中获取 fso,因为这是存储信息的地方。这个 fso 是不同的,具体取决于实体反射器类发送到这个 javascript 的内容。我有一个函数可以格式化类似于我发现的 C# 的 javascript 字符串。然后我有另一个函数从实体反射器类中获取 fso。这有效。我通过在警报中显示它来测试字符串,这工作正常。我遇到的问题是使用 javascript 设置锚标记的 href。我快疯了!请帮忙!
String Format:
字符串格式:
String.format = function() {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\{" + i + "\}", "gm");
s = s.replace(reg, arguments[i + 1]);
}
}
My attempt to change the href:
我尝试更改 href:
function changeHref(fso) {
var downloadHref = String.format("Download.ashx?fso={0}", fso);
$('#<%= this.downloadLink.ClientID %>').href = downloadHref;
showDialog(<%= this.'custom dialog i made'.ClientID %>);
}
The download link is changed and everything. I just cannot seem to be able to set this! Am I missing the order of the page load? Do I need to do this after the whole page loads since items might not be generated yet? I tried a couple different things. I really could use a direction.
下载链接已更改,一切都已更改。我似乎无法设置它!我错过了页面加载的顺序吗?由于项目可能尚未生成,我是否需要在整个页面加载后执行此操作?我尝试了几种不同的方法。我真的可以使用一个方向。
采纳答案by Josh Stodola
You can't reference hrefdirectly like that from a jQuery object. All you are doing is creating a new property. Change it to set the attribute through attrlike this...
您不能href像从 jQuery 对象那样直接引用。您所做的只是创建一个新属性。更改它以通过attr这样设置属性...
$('#<%= this.downloadLink.ClientID %>').attr("href", downloadHref);
For completeness, I should mention that you canget to the underlying DOM element by using array syntax, and then you could set the href with regular Javascript...
为了完整起见,我应该提到您可以使用数组语法访问底层 DOM 元素,然后您可以使用常规 Javascript 设置 href ......
var domElem = $('#<%= this.downloadLink.ClientID %>')[0]; // DOM element is at 0
domElem.href = downloadHref;
Also, another probable error, I think you need quotes here...
另外,另一个可能的错误,我认为你需要在这里引用...
showDialog("<%= this.'custom dialog i made'.ClientID %>");
回答by Dustin Laine
This line is incorrect, to set the HREFyou need to access the attrfunction of jQuery.
这行不正确,设置HREF需要访问attrjQuery的功能。
$('#<%= this.downloadLink.ClientID %>').attr("href", downloadHref);
回答by Diodeus - James MacFarlane
Your javascript needs to run after the page load is complete.
您的 javascript 需要在页面加载完成后运行。
The jQuery way of doing it:
jQuery 的做法:
$(document).ready(function() { init() })
function init() {
$('#<%= this.downloadLink.ClientID %>').attr("href", downloadHref);
//Josh's code above
}

