請幫助我將我的功能添加到所有div。我想用'each',但不知道如何。我試圖使用$('div')。test()函數時,我只需要2個警告消息,但我只成爲第一個div。它仍然有效。如何使用我自己創建的多個項目的jQuery函數?
<html>
<head>
<style type="text/css">
div {
width:100px;
height:100px;
background: yellow;
float: left;
margin: 20px;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
<div id="a"></div>
<div id="b"></div>
<script type="text/javascript">
(function($){
$.fn.test = function() {
alert(this.attr('id'));
};
})(jQuery);
//work
$('#a').test();
// not work
//$('div').test();
</script>
</body>
</html>
Thanx!
等都不是幫助論壇,請嘗試以不太特定的方式重新提出您的問題,更有可能幫助其他用戶解決同樣的問題。 – OneChillDude
@ bwheeler96問題有什麼問題?似乎對我很清楚 – charlietfl