onChange和onClick的区别
change和onchange、click和onclick的区别:onchange和onclick都是js方法也可以 jsobject.onchange=function(){SomeJavaScriptCode};change和click是jquery方法$('').change(function({}))或者; $('').click(function({}))
jQuery通用的全局遍历方法$.each()用法实例
1.test.json文件代码:
[
{
"username": "张三",
"content": "沙发."
},
{
"username": "李四",
"content": "板凳."
},
{
"username": "王五",
"content": "地板."
}
]
2.html代码:
<p>
<input type="button" id="send" value="加载"/>
</p >
<div >已有评论:</div>
<div id="resText" ></div>
3.jQuery代码:
<script src="jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
/*
1.$.each()是jquery的一个通用的遍历方法,可用于遍历对象和数组
2.$.each()函数不同于jquery对象的each()方法,它是一个全局函数,不操作jquery对象,而是以一个数组或者对象作为第一个参数,以一个回调函数作为第二个参数。回调函数拥有两个参数:第一个参数为对象的成员或数组的索引,第二个参数为对应变量或内容
*/
$(function(){
$('#send').click(function() {
$.getJSON('test.json', function(data) {
$('#resText').empty();
var html = '';
$.each( data , function(commentIndex, comment) {
html += '<div ><h6>' + comment['username'] + ':</h6><p >' + comment['content'] + '</p ></div>';
})
$('#resText').html(html);
})
})
})
</script>
表格怎么设置点击哪一行有颜色
在表格中设置点击某一行后该行变色,可以通过以下步骤实现:
在CSS中定义选中行的样式,例如:
tr.selected {
background-color: #ccc;
}
使用JavaScript或jQuery为表格的每一行添加点击事件,当点击某一行时,为该行添加选中样式,并移除其他行的选中样式。例如:
// 使用JavaScript实现
var rows = document.getElementsByTagName("tr");
for (var i = 0; i < rows.length; i++) {
rows[i].onclick = function() {
// 移除其他行的选中样式
for (var j = 0; j < rows.length; j++) {
rows[j].classList.remove("selected");
}
// 为当前行添加选中样式
this.classList.add("selected");
}
}
// 使用jQuery实现
$("tr").click(function() {
// 移除其他行的选中样式
$("tr").removeClass("selected");
// 为当前行添加选中样式
$(this).addClass("selected");
});
这样,当用户点击某一行时,该行就会变成选中状态,其他行则恢复为默认状态。
发表评论