javascript 如何为链接创建onclick
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15001413/
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
how to create onclick for a link
提问by Java Questions
i have a div
for which i set value dynamically during run time, if there is value than i have enable or create a link which will have onclick
method where i will call a javascript
method.
我有一个div
我在运行时动态设置的值,如果有值比我启用或创建一个链接,该链接将具有onclick
我将调用javascript
方法的方法。
how to do this in jquery
or javascript
?
如何做到这一点的jquery
还是javascript
?
I set value to a div
like the following,
我将值设置div
为如下所示,
document.getElementById('attachmentName').innerHTML=projectInforamtionMap.Cim_AttachmentNames;
this the div :
这是 div :
<tr>
<td align="left"><div id="attachmentName"> </div></td>
</tr>
Kindly help me to find and fix.
请帮我找到并修复。
Best Regards
最好的祝福
采纳答案by Jefferson Henrique C. Soares
You can set a onclick function:
你可以设置一个onclick函数:
document.getElementById('attachmentName').onclick = function() {};
回答by Dineshkani
Assume that your function are previously define like this
假设您的函数之前是这样定义的
function foo(){alert('function call');}
After adding innerHTML
添加innerHTML后
In Javascript
在 JavaScript 中
document.getElementById('attachmentName').setAttribute('onclick','foo()');
In Jquery
在jQuery中
$("#attachmentName").attr('onclick','foo()');
回答by K D
You can do this without inserting link in the div in following way
您可以通过以下方式在 div 中不插入链接的情况下执行此操作
document.getElementById("attachmentName").onClick = function () {
alert(1); // To test the click
};
You can achieve it with the help of jQuery as well in the following way.
您也可以通过以下方式在 jQuery 的帮助下实现它。
$("#attachmentName").click(function ()
{
alert(1);
});
for this you have to include jQuery liabray on the page. refer jQuery.com
为此,您必须在页面上包含 jQuery liabray。参考 jQuery.com
Still if you forecefully want to include the link in Div then following is the code for it
尽管如此,如果您强烈希望在 Div 中包含链接,那么以下是它的代码
var link = $("<a>"+ $("#attachmentName").text() +"</a>");
$("#attachmentName").html($(link);
link.click(function () {
alert(1);
});
Hope this would help.
希望这会有所帮助。
回答by albertoblaz
You have several alternatives
你有几个选择
JavaScript:
JavaScript:
// var elem = document.getElementById('attachmentName');
elem.onclick = function() {};
elem.setAttribute('onclick','foo()');
elem.addEventListener('onclick', foo, false); // The most appropiate way
jQuery:
jQuery:
// var elem = $('#attachmentName');
elem.click(foo);
elem.on('click', foo);
$('body').on('click', elem, foo);
Between the 3 jQuery alternatives, the last is the best one. The reason is due to the fact that you are attaching an event just the body element. In this case, it does not matter but in other cases, you are probably willing to attach the same event to a collection of elements, not just one.
在 3 个 jQuery 替代方案中,最后一个是最好的。原因是因为您仅将事件附加到 body 元素。在这种情况下,这无关紧要,但在其他情况下,您可能愿意将同一事件附加到一组元素,而不仅仅是一个。
Therefore, using this approach, the event is attached to the body but works for the elements clicked, instead of attaching the same event to every element, so it's more efficient :)
因此,使用这种方法,事件附加到正文但适用于单击的元素,而不是将相同的事件附加到每个元素,因此效率更高:)
回答by EnterJQ
$('#attachmentName').click(function(){
//do your stuff
})
回答by Sudhir Bastakoti
jquery way:
jquery方式:
$("#attachmentName").click(function() {
some_js_method();
});
回答by civilator
as i was converting a classical asp details page to a one-page ajaxified page, i converted the existing links in the page to get the details loaded in the DIV in the same page. i renamed the href tag to dtlLink and got that data in the jquery load() function.
当我将经典的 asp 详细信息页面转换为一页 ajaxified 页面时,我转换了页面中的现有链接以获取在同一页面中的 DIV 中加载的详细信息。我将 href 标签重命名为 dtlLink 并在 jquery load() 函数中获取该数据。
detail.asp (server.urlencode is added and required here )
detail.asp (server.urlencode 是在这里添加和需要的)
<a href=""#"" onclick=""javascript:LoadDetails(this)"" dtllink="detail.asp?x=" & Server.URLEncode(Request("x"))&y=" & Server.URLEncode(Request("y")) > link text </a>
parent.asp (it has some extra code for holdon.js spinner image, button enable/disable, results div show/hide) Jquery
parent.asp(它有一些额外的代码用于holdon.js 微调图像、按钮启用/禁用、结果 div 显示/隐藏)Jquery
function LoadDetails(href) {
//get the hyperlink element's attribute and load it to the results div.
var a_href = $(href).attr('dtllink');
console.log(a_href);
$('#divResults').html('');
DisplayHoldOn();
$('#divResults').load(a_href, function (response, status, xhr) {
$('#divCriteria').hide();
HoldOn.close();
if (status == "error") {
var msg = "There was an error: ";
$("#divResults").html(msg + xhr.status + " " + xhr.statusText);
$('#divCriteria').show();
$("#cmdSubmit").removeAttr('disabled');
}
});
}