使用this.addClass(Jquery)

Roh*_*eep 3 javascript jquery

我在jquery中尝试this.addclass将一个类添加到DIV,这可能是未知的.这可以用这种方式解决吗?

<style>.classOne { font-size:24px; color:#009900;}</style>

<script>
function hello()
{   alert();
$(this).addClass('classOne');
}
</script>

<div class="something"  onclick="hello();"> Test information </div>
Run Code Online (Sandbox Code Playgroud)

ade*_*neo 12

尽可能避免使用内联javascript,并执行以下操作:

<style>.classOne { font-size:24px; color:#009900;}</style>

<script type="text/javascript">
$(function() {
    $('.something').on('click', function() {
        alert('hello');
        $(this).addClass('classOne');
    });
});
</script>

<div class="something"> Test information </div>
Run Code Online (Sandbox Code Playgroud)

小提琴


Ria*_*ers 5

您对“ this”的引用无效,请尝试使用此方法

<style>.classOne { font-size:24px; color:#009900;}</style>

<script>
function hello(ojecttRef)
{   alert();
$(ojecttRef).addClass('classOne');
}
</script>

<div class="something"  onclick="hello(this);"> Test information </div>
Run Code Online (Sandbox Code Playgroud)