2013-02-21 113 views
4

我有一个div其中我运行时动态设置值,如果有价值比我有启用或创建一个链接,将有onclick方法,我将调用javascript方法。如何创建一个链接的onclick

如何在jqueryjavascript中做到这一点?

我设定值的div像下面,

这个DIV:

<tr> 
    <td align="left"><div id="attachmentName"> </div></td> 
</tr> 

请帮我找到并修复。

问候

+0

在javascript中使用.setAttribute。 – Dineshkani 2013-02-21 11:46:13

回答

4

您可以设置的onclick功能:

document.getElementById('attachmentName').onclick = function() {}; 
+0

我只是把警报,但它不适用于所有给出的答案 – 2013-02-21 12:03:23

+0

你可以在JsFiddle上做一个样本吗? – 2013-02-21 12:04:13

+1

看这个示例[JsFiddle](http://jsfiddle.net/gRgfa/) – 2013-02-21 12:06:39

1
$('#attachmentName').click(function(){ 
    //do your stuff 
}) 
1

jQuery的方式:

$("#attachmentName").click(function() { 
    some_js_method(); 
}); 
1

假设你的函数是以前这样定义

function foo(){alert('function call');} 

添加的innerHTML

在Javascript中

document.getElementById('attachmentName').setAttribute('onclick','foo()'); 

jQuery中

$("#attachmentName").attr('onclick','foo()'); 
1

后,你可以做到这一点没有在下列方式

document.getElementById("attachmentName").onClick = function() { 
                    alert(1); // To test the click 
                   }; 

您可以在插入在div链接用它来实现它以下面的方式帮助jQuery。

$("#attachmentName").click(function() 
             { 
              alert(1); 
             }); 

为此,您必须在页面上包含jQuery liabray。参考jQuery.com

不过,如果你forecefully想在事业部则下面的链接是它

var link = $("<a>"+ $("#attachmentName").text() +"</a>"); 
$("#attachmentName").html($(link); 
link.click(function() { 
          alert(1); 
         }); 

希望这将有助于代码。

1

您有几种选择

的JavaScript:

// var elem = document.getElementById('attachmentName'); 
elem.onclick = function() {}; 
elem.setAttribute('onclick','foo()'); 
elem.addEventListener('onclick', foo, false); // The most appropiate way 

的jQuery:

// var elem = $('#attachmentName'); 
elem.click(foo); 
elem.on('click', foo); 
$('body').on('click', elem, foo); 

3个jQuery的选择之间,最后一个是最好的之一。原因是因为你附加一个事件只是body元素。在这种情况下,这并不重要,但在其他情况下,您可能愿意将同一事件附加到一组元素,而不仅仅是一个。

因此,使用这种方法,该事件被连接到主体,但适用于元素点击,而不是相同的事件附接至每一个元素,所以它的更有效的:)

0

如我被转换古典asp详细信息页面转换为单页面ajaxified页面,我转换页面中的现有链接以获取在同一页面中加载到DIV中的详细信息。我将href标签重命名为dtlLink,并在jquery load()函数中获取了这些数据。

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(它有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'); 
       } 
      }); 
     }