使用 javascript 更改锚标记的 href
嘿,我有一个关于 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'>
现在我必须从数据库获取 fso,因为那是存储信息的地方。此 fso 有所不同,具体取决于实体反射器类发送到此 javascript 的内容。我有一个函数可以格式化 javascript 字符串,类似于我发现的 C#。然后我有另一个函数从实体反射器类获取 fso。这有效。我通过在警报中显示该字符串来测试该字符串,效果很好。我遇到的问题是使用 javascript 设置锚标记的 href。我要疯了!请帮忙!
字符串格式:
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]);
}
}
我尝试更改 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 %>);
}
下载链接已更改以及所有内容。我似乎无法设置这个!我错过了页面加载的顺序吗?由于项目可能尚未生成,我是否需要在整个页面加载后执行此操作?我尝试了几种不同的方法。我真的需要一个方向。
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.
<'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!
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能像从 jQuery 对象那样直接引用
href
。您所做的只是创建一个新属性。将其更改为通过attr
设置属性,如下所示...为了完整性,我应该提到,你可以使用数组语法获取底层 DOM 元素,然后你可以使用常规 Javascript 设置 href...
另外,另一个可能的错误,我认为你需要在这里引号...
You can't reference
href
directly like that from a jQuery object. All you are doing is creating a new property. Change it to set the attribute throughattr
like this...For completeness, I should mention that you can get to the underlying DOM element by using array syntax, and then you could set the href with regular Javascript...
Also, another probable error, I think you need quotes here...
此行不正确,要设置
HREF
,您需要访问 jQuery 的attr
函数。This line is incorrect, to set the
HREF
you need to access theattr
function of jQuery.您的 JavaScript 需要在页面加载完成后运行。
jQuery 的实现方式:
Your javascript needs to run after the page load is complete.
The jQuery way of doing it: