javascript – jQuery:html()在事件中不可用?

前端之家收集整理的这篇文章主要介绍了javascript – jQuery:html()在事件中不可用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
新手问题:

我有一个带有单个p元素的网页.我以为我可以访问事件中的p.html(),但不能.有没有办法作为jQuery元素访问事件内的元素?

<script src="Scripts/jquery-1.8.2.js"></script>      
    $(function () {
        $("p").first().bind('click',null,function (event) {
            this.innerHTML = "this works";
            this.html('this does not');
        });
    }
    );

解决方法

这在处理函数中是你的DOM元素..它没有.html函数..用$包装它使它成为一个jQuery对象并做一个.html

$(this).html('this does work now');

完整代码

$(function () {
    $("p").first().bind('click',function (event) {
       this.innerHTML = "this works";
       $(this).html('this does work now');
    });
});

猜你在找的jQuery相关文章