Heim  >  Artikel  >  Web-Frontend  >  jquery给HTML元素添加 事件_html/css_WEB-ITnose

jquery给HTML元素添加 事件_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 12:06:38989Durchsuche

一段html代码中  
用js或jquey怎么变成  


回复讨论(解决方案)

给按钮添加一个ID
 

Js--JQ:
$("#bt1").attr("onclick",function(){
   alert("111");
})

大概这样

只要你能找到这个元素,事件还是很好加的

$('button').click(function(){
});

给按钮添加一个ID
 

Js--JQ:
$("#bt1").attr("onclick",function(){
   alert("111");
})

大概这样

用这个方法,为什么我追加 这段html代码段 的时候就 执行 alert,而追加后的 “点击” 按钮就不执行了


给按钮添加一个ID
 

Js--JQ:
$("#bt1").attr("onclick",function(){
   alert("111");
})

大概这样

用这个方法,为什么我追加 这段html代码段 的时候就 执行 alert,而追加后的 “点击” 按钮就不执行了

那可以看你是使用的jq哪个版本 具体的看文档吧
低版本可以用

$("#bt1").live("click",function(){});

高版本用:
$("body").on("click","#bt1",function(){});



给按钮添加一个ID
 

Js--JQ:
$("#bt1").attr("onclick",function(){
   alert("111");
})

大概这样

用这个方法,为什么我追加 这段html代码段 的时候就 执行 alert,而追加后的 “点击” 按钮就不执行了

那可以看你是使用的jq哪个版本 具体的看文档吧
低版本可以用

$("#bt1").live("click",function(){});

高版本用:
$("body").on("click","#bt1",function(){});



为什么是事件绑定的时候就调用了,而不是点击按钮时候调用的


给按钮添加一个ID
 

Js--JQ:
$("#bt1").attr("onclick",function(){
   alert("111");
})

大概这样

用这个方法,为什么我追加 这段html代码段 的时候就 执行 alert,而追加后的 “点击” 按钮就不执行了

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title></head><script type="text/javascript" src="jquery-1.js"></script><script type="text/javascript">  var sa =  $("#bt1");   function ale(){	     //alert(sa)		 document.getElementById('bt1').setAttribute('onclick','gogogo()')	 	   }	 function  gogogo(){		     alert("ou yeah");		 }</script><body><input  type="button" id="bt1" value="123"/><input type="button" id="bt2"  value="123312" onclick="ale()" /></body></html>
已测试 可以使用  记得导入JQ

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn