说我有两个图像和两个div。
<html>
<body>
<img id="div0image" class='divImages' alt="" src="../images/div0image.png" />
<img id="div1image" class='divImages' alt="" src="../images/div1image.png" />
<div id="div0" class='divs'></div>
<div id="div1" class='divs'></div>
</body>
</html>
假设CSS是这样的
#div0, #div1, #div0image, #div1image {
width: 200px;
width: 200px;
}
现在,说我有这样的
function somethingIsClicked(thisParameter) { //thisParameter needs to be a $(this) object
var thisID = $(this).attr('id'); //$(this) should refer to the object passed in the parameter (thisParameter)
$(this).addClass('checkIfThisClassIsAdded');
alert('works!');
}
然后一个JavaScript函数假设我有这两个功能
$('.divs').click(function() {
somethingIsClicked($(this));
});
$('.divImages').click(function() {
thisID = $(this).attr('id'); //div0image
thisDivsID = thisID.slice(0,4); //div0
$('#' + thisDivsID).each(function() {
somethingIsClicked($(this));
});
是否有正确的方法来传递,然后接收$(this)作为参数,然后引用$(this)作为参数的函数中的$(this)对象?
P.S. '#div0,div1,div0image,div1image'应该是'#div0,#div1,#div0image,#div1image'。 –