单击Javascript以获取超链接的ID

时间:2023-01-08 21:19:50

I would like to know how i can set the current hyperlink id to a hidden field on clicking the corresponding links. The html control code is as follows:

我想知道如何在单击相应链接时将当前超链接ID设置为隐藏字段。 html控件代码如下:

<a href="#TB_inline?height=155&width=300&inlineId=hiddenModalContent" class="thickbox" id="ExpressionsLink"
          title="Create expression column" onclick="keepID()">Add Expressions Model</a> 
           <a href="#TB_inline?height=155&width=300&inlineId=hiddenModalContent" class="thickbox" id="AggregateMethodLink"
          title="Create aggregate column">Add Aggregate Methods</a><input id="HiddenIdHolder" 
          type="hidden" />

I need the id of the link clicked on the hidden field 'HiddenIdHolder'.

我需要点击隐藏字段'HiddenIdHolder'的链接ID。

Javascript

使用Javascript

 function keepID() {
     var hiddenInput = document.getElementById("HiddenIdHolder");
     hiddeninput.value= ? // What can i do here to get the id?
 }

3 个解决方案

#1


3  

this refers to the element itself. Example on jsFiddle

这指的是元素本身。关于jsFiddle的示例

onclick="keepID(this)"

Then

然后

function keepID(element)
{
    var hiddenInput = document.getElementById("HiddenIdHolder");
    hiddeninput.value = element.getAttribute("id");
}

#2


0  

Use jQuery:

使用jQuery:

$('a').click(function() {
    $('#HiddenIdHolder').val($(this).attr('id'))
})

#3


0  

You should modify your HTML to provide argument for KeepID function:

您应该修改HTML以提供KeepID函数的参数:

<a href="#xxx" onclick="KeepID(this);">ooxx</a>

note that you should provide a this argument when invoke KeepID function, then in KeepID function, you can access this element from argument:

请注意,在调用KeepID函数时应该提供this参数,然后在KeepID函数中,您可以从参数中访问此元素:

function KeepID(src) {
    document.getElementById("HiddenIdHolder").value = src.id;
}

#1


3  

this refers to the element itself. Example on jsFiddle

这指的是元素本身。关于jsFiddle的示例

onclick="keepID(this)"

Then

然后

function keepID(element)
{
    var hiddenInput = document.getElementById("HiddenIdHolder");
    hiddeninput.value = element.getAttribute("id");
}

#2


0  

Use jQuery:

使用jQuery:

$('a').click(function() {
    $('#HiddenIdHolder').val($(this).attr('id'))
})

#3


0  

You should modify your HTML to provide argument for KeepID function:

您应该修改HTML以提供KeepID函数的参数:

<a href="#xxx" onclick="KeepID(this);">ooxx</a>

note that you should provide a this argument when invoke KeepID function, then in KeepID function, you can access this element from argument:

请注意,在调用KeepID函数时应该提供this参数,然后在KeepID函数中,您可以从参数中访问此元素:

function KeepID(src) {
    document.getElementById("HiddenIdHolder").value = src.id;
}