使用 javascript 更改锚标记的 href

发布于 2024-09-28 18:48:59 字数 1345 浏览 3 评论 0原文

嘿,我有一个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘然心甜 2024-10-05 18:48:59

您不能像从 jQuery 对象那样直接引用 href。您所做的只是创建一个新属性。将其更改为通过 attr 设置属性,如下所示...

$('#<%= this.downloadLink.ClientID %>').attr("href", downloadHref);

为了完整性,我应该提到,你可以使用数组语法获取底层 DOM 元素,然后你可以使用常规 Javascript 设置 href...

var domElem = $('#<%= this.downloadLink.ClientID %>')[0]; // DOM element is at 0
domElem.href = downloadHref;

另外,另一个可能的错误,我认为你需要在这里引号...

showDialog("<%= this.'custom dialog i made'.ClientID %>");

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 through attr like this...

$('#<%= this.downloadLink.ClientID %>').attr("href", downloadHref);

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...

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 %>");
晨曦÷微暖 2024-10-05 18:48:59

此行不正确,要设置 HREF,您需要访问 jQuery 的 attr 函数。

$('#<%= this.downloadLink.ClientID %>').attr("href", downloadHref);

This line is incorrect, to set the HREF you need to access the attr function of jQuery.

$('#<%= this.downloadLink.ClientID %>').attr("href", downloadHref);
一个人的旅程 2024-10-05 18:48:59

您的 JavaScript 需要在页面加载完成后运行。

jQuery 的实现方式:

$(document).ready(function() { init() })


function init() {
  $('#<%= this.downloadLink.ClientID %>').attr("href", downloadHref);
  //Josh's code above

}

Your javascript needs to run after the page load is complete.

The jQuery way of doing it:

$(document).ready(function() { init() })


function init() {
  $('#<%= this.downloadLink.ClientID %>').attr("href", downloadHref);
  //Josh's code above

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文