我在https://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_sel_this中看到了一个使用“ this”的例子
$(document).ready(function() {
$("button").click(function() {
$(this).hide();
}
);
}
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
但是当我尝试创建单独的函数时,它无法正常工作,我认为指针“ this”是问题所在,但我不知道如何解决此问题。
$(document).ready(function() {
$("button").click(hidden())
}
);
function hidden() {
$(this).hide()
}
;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>