[英]How should I use JQuery to change the background color of a DOM element on mouse over?
是否可以在鏈接中執行jQuery的代碼? 例如,我可以這樣做嗎?
<a href="index.php" onmouseover="$(this).css("background-color","#111111");">Home</a>
我想在某些鏈接中使用它來創建某種效果,而不要觸摸更多CSS。
您正在用JavaScript中的引號關閉屬性引號。
要么逃避他們,
onmouseover="$(this).css(\"background-color\",\"#111111\");"
或對內部或外部使用單引號。
onmouseover='$(this).css("background-color","#111111");'
你要這個:
<script type="text/javascript">
$(function(){
$("a").hover(
function () {
$(this).addClass("hover");
},
function () {
$(this).removeClass("hover");
}
);
});
</script>
<style type="text/css">
.hover{
background-color: #111111;
}
</style>
<a href="index.php">Home</a>
您可以在以下位置找到其他資源: http : //api.jquery.com/hover/
$('document').ready(function() {
$('a').hover(function() {
// on hover
$(this).css("background-color","#111111");
}, function() {
// on mouse leave
// code
});
});
<a href="index.php" onmouseover="$(this).css('background-color','red');">Home</a>
將起作用。
您曾經onmouseover="$(this).css("background-color","#111111");"
我使用了onmouseover="$(this).css('background-color','red');"
演示位於http://jsfiddle.net/2dJAN/44/
注意:您可以使用像http://jsfiddle.net/2dJAN/45/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.