js文件
function tableTrHover(tableId) {
$("#"+tableId+">tr").hover(function() {
$(this).addClass("hover"); // 鼠标经过添加hover样式
}, function() {
$(this).removeClass("hover"); // 鼠标离开移除hover样式
})
}
页面文件
$(document).ready(tableTrHover("test"));.......
补充:为什么会有tbody,这是浏览器解析的结果。
[code="java"] $(document).ready(function(){tableTrHover("test");}); [/code]
ready函数,看官网的实例:http://api.jquery.com/ready/
[code="js"]function tableTrHover(tableId) {
$("#"+tableId+">tr").hover(function() {
$(this).addClass("hover"); // 鼠标经过添加hover样式
}, function() {
$(this).removeClass("hover"); // 鼠标离开移除hover样式
})
}
[/code]
这个放在ONREADY里试试?
丢了分号。
[code="jquery"]function tableTrHover(tableId) {
$("#"+tableId+">tr").hover(function() {
$(this).addClass("hover"); // 鼠标经过添加hover样式
}, function() {
$(this).removeClass("hover"); // 鼠标离开移除hover样式
});
} [/code]
[code="js"]$(document).ready(function(){
function tableTrHover(tableId) {
$("#"+tableId+">tr").hover(function() {
$(this).addClass("hover"); // 鼠标经过添加hover样式
}, function() {
$(this).removeClass("hover"); // 鼠标离开移除hover样式
})
}
tableTrHover("test");
}
); [/code]
这样写
或者这样写
[code="js"] function tableTrHover(tableId) {
$("#"+tableId+">tr").hover(function() {
$(this).addClass("hover"); // 鼠标经过添加hover样式
}, function() {
$(this).removeClass("hover"); // 鼠标离开移除hover样式
})
}
$(document).ready(function(){
tableTrHover("test");
}[/code]
[color=red][quote] $(document).ready(tableTrHover("test")); [/quote][/color]
[color=blue]
[b]你的错误在这句话,document.ready 后面应该是一个 Callback函数,而你这样写不是Callback函数,而是 调用函数。[/b][/color]
[b]这样就可以了:[/b]
[code="js"] $(document).ready( function (){
tableTrHover("test");
});
function tableTrHover(tableId) {
$("#"+tableId+" tr").hover(function() {
$(this).addClass("hover"); // 鼠标经过添加hover样式
}, function() {
$(this).removeClass("hover"); // 鼠标离开移除hover样式
});
}[/code]
[code="java"]function tableTrHover(tableId) {
$("#"+tableId+" tbody>tr").hover(function() {
$(this).addClass("hover"); // 鼠标经过添加hover样式
}, function() {
$(this).removeClass("hover"); // 鼠标离开移除hover样式
}) ;
} [/code]
table的子孩子为tbody。
这样写
[code="java"]$("#"+tableId+" > tbody > tr").hover(......[/code]
因为TABLE下面自动多了一级tbody
你出错的根本原因是你没理解 $(document).ready(); jquery的ready方法后面应该写函数名,而不是调用函数
再定义一个函数
function tableTrHover(tableId) {
$("#"+tableId+">tr").hover(function() {
$(this).addClass("hover"); // 鼠标经过添加hover样式
}, function() {
$(this).removeClass("hover"); // 鼠标离开移除hover样式
})
}
function onloadTableTrHover() {
tableTrHover("test");
}
$(document).ready(onloadTableTrHover);