2012-09-15 85 views
1

我需要一些帮助,这个jQuery代码。我有一个SVG地图,我需要一个点击一个国家来选择一个国家,并取消选择另一个。有人能帮助我吗? :) 这是代码:jquery点击功能选择并取消选择

<script type="text/javascript">//<![CDATA[ 
$(window).load(function(){ 
$(document).ready(function() { 




$("path").mouseover(function() { 
    $(this).css('cursor', 'pointer');$(this).attr({ 
    fill: "#FF0000", 
    stroke: "#00FF00" 
    }); 
    $(this).css('cursor', 'pointer'); 
    }) 
.mouseout(function(){ 
    if(!$(this).data('clicked')){ 
     $(this).attr({ 
    fill: "#FFFFFF", 
    stroke: "#eee" 
    }); 
    } 
    }); 


$("path").click(function() { 
$(this).attr({ 
    fill: "#FF0000", 
    stroke: "#00FF00" 
}); 
$(this).data('clicked', true); 

}); 

}); 
});//]]> 

</script> 
+0

时加入一些类你不解释你得到的是什么问题或错误,这是一个很糟糕的问题,只是粘贴代码。 – Nelson

回答

2

最有力的方法是使用:http://api.jquery.com/toggle-event/

+0

我已经用自定义代码解决了这个问题,但直到今天我还不知道'toggle()'。为什么它被标记为废弃,但? – jimp

+0

哎呀......这在文档页面上不太可见。检查这里:http://stackoverflow.com/questions/4911577/jquery-click-toggle-between-two-functions –

+0

这里是死刑判决:http://bugs.jquery.com/ticket/11786 –

0

你可能会选择再当去除类取消

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title></title> 
</head> 
<body> 
    <input type='text' id="text1" onclick='javascript:toggleClass(this)' /> 
</body> 
</html> 
<script src="jquery-1.2.6.min.js" type="text/javascript"></script> 
<script language='javascript' type="text/javascript"> 


function toggleClass(item) { 

    if ($(item).hasClass('select')) { 
     $(item).removeClass('select').addClass('deselect'); 
    } 
    else { 
     $(item).removeClass('deselect').addClass('select'); 
    } 

    } 
</script>