您的位置:首页 > Web前端 > JQuery

用闭包解决jQuery和其它框架的$符号冲突的问题

2011-06-24 16:31 549 查看
直接上代码吧,感兴趣的自己下载回去测试,自定义的$符号和jQuery的$符号混用成功

<!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>
<body>
name:<span id="s1"></span><br/>
name is:<input type="text" name="myname" id="myname" value="" style="width:200px;" />
<br/>
<button id="b1">自定义的$</button>
<button id="b2">jQuery的$</button>
</body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function $(id){return document.getElementById(id);}
</script>
<script type="text/javascript">
setInterval(function(){document.getElementById("s1").innerHTML=new Date().toLocaleString();},1000);
document.getElementById("myname").value="";
(function($){
$("#b2").click(function(){$("#myname").val($("#s1").html());});
})(jQuery);
$("b1").onclick=function(){$('myname').value=$('s1').innerHTML;}
</script>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: