jQuery มีฟังก์ชั่น hasClass () ที่ส่งกลับจริงถ้าองค์ประกอบใด ๆ ในชุดห่อมีชั้นที่ระบุ
if (!$(this).hasClass("selected")) {
//do stuff
}
ดูตัวอย่างการใช้งานของฉัน
- หากคุณโฮเวอร์เหนือ div มันจะจางหายไปตามความเร็วปกติถึง 100% opacity หาก div ไม่มีคลาส 'ที่เลือก'
- หากคุณโฮเวอร์จาก div มันจะจางหายไปด้วยความเร็วช้าถึงความทึบ 30% หาก div ไม่มีคลาสที่ถูกเลือก
- การคลิกปุ่มจะเพิ่มคลาส 'ที่เลือก' ให้กับ div สีแดง เอฟเฟกต์สีซีดจางไม่สามารถใช้งานกับ div สีแดงได้อีกต่อไป
นี่คือรหัสสำหรับมัน
<!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" xml:lang="en" lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<title>Sandbox</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
body { background-color: #FFF; font: 16px Helvetica, Arial; color: #000; }
</style>
<!-- jQuery code here -->
<script type="text/javascript">
$(function() {
$('#myButton').click(function(e) {
$('#div2').addClass('selected');
});
$('.thumbs').bind('click',function(e) { alert('You clicked ' + e.target.id ); } );
$('.thumbs').hover(fadeItIn, fadeItOut);
});
function fadeItIn(e) {
if (!$(e.target).hasClass('selected'))
{
$(e.target).fadeTo('normal', 1.0);
}
}
function fadeItOut(e) {
if (!$(e.target).hasClass('selected'))
{
$(e.target).fadeTo('slow', 0.3);
}
}
</script>
</head>
<body>
<div id="div1" class="thumbs" style=" background-color: #0f0; margin: 10px; padding: 10px; width: 100px; height: 50px; clear: both;">
One div with a thumbs class
</div>
<div id="div2" class="thumbs" style=" background-color: #f00; margin: 10px; padding: 10px; width: 100px; height: 50px; clear: both;">
Another one with a thumbs class
</div>
<input type="button" id="myButton" value="add 'selected' class to red div" />
</body>
</html>
แก้ไข:
นี่เป็นเพียงการคาดเดา แต่คุณพยายามทำสิ่งนี้ให้สำเร็จหรือไม่?
- divs ทั้งสองเริ่มต้นที่ความทึบแสง 30%
- โฮเวอร์เหนือ div จะจางหายไปเป็น 100% opacity, hovering out จางหายไปเป็น opacity 30% เฟดเอฟเฟกต์ใช้งานได้กับองค์ประกอบที่ไม่มีคลาส 'ที่เลือก' เท่านั้น
- การคลิก div เพิ่ม / ลบคลาส 'ที่เลือก'
รหัส jQuery อยู่ที่นี่ -
$(function() {
$('.thumbs').bind('click',function(e) { $(e.target).toggleClass('selected'); } );
$('.thumbs').hover(fadeItIn, fadeItOut);
$('.thumbs').css('opacity', 0.3);
});
function fadeItIn(e) {
if (!$(e.target).hasClass('selected'))
{
$(e.target).fadeTo('normal', 1.0);
}
}
function fadeItOut(e) {
if (!$(e.target).hasClass('selected'))
{
$(e.target).fadeTo('slow', 0.3);
}
}
<div id="div1" class="thumbs" style=" background-color: #0f0; margin: 10px; padding: 10px; width: 100px; height: 50px; clear: both; cursor:pointer;">
One div with a thumbs class
</div>
<div id="div2" class="thumbs" style=" background-color: #f00; margin: 10px; padding: 10px; width: 100px; height: 50px; clear: both; cursor:pointer;">
Another one with a thumbs class
</div>