需要准备的材料分别有:电脑、html编辑器、浏览器。
1、首先,打开html编辑器,新建html文件,例如:index.html,编写问题基础代码。
2、在index.html中的<script>标签,输入js代码:$('div').click(function(){$(this).css('color','blue')})。
3、浏览器运行index.html页面,此时点击123所在的div,div会变为蓝色文本。
可以先给div标签设置id,然后给这个id绑定点击事件。
1、新建html文档,在body标签中添加一个div标签,为div标签设置一个id,这里以“demo”为例:
2、在head标签中引入jquery的js文件,这时可以使用cdn链接:
3、添加script标签,在script标签中绑定“demo”,然后使用click函数为div标签绑定点击事件:
<button id="btn">hahaha</button>
<div id="t"></div>
<button id="btn">hahaha</button>
<div id="t"></div>
<script type="text/javascript" src="jquery.js"></script>
<script>
function hello(){
alert("hello!")
}
$("#t").html('<div onclick="hello()">hello</div>')
</script>
可能是你的代码先后顺序有问题,我这里测试没问题。js要在div t的后面。另外onclick事件不需要在里面加javascript它本身就是执行一个js函数,如果是a标签href里就需要加。如果你的js一定要写在前面也可以把你的代码写入:
$(document).reday(function(){$("#t").html('<div onclick="hello()">hello</div>')
})
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)