Jquery没有使用像Jackate这样的插件来为dom添加新添加的元素 [英] Jquery is not working on newly added elements to the dom with Plugin like JRate Plugin

查看:96
本文介绍了Jquery没有使用像Jackate这样的插件来为dom添加新添加的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在互联网上搜索这个,我找到了一些有用的答案,但它们还不足以解决我的问题(例如
类似问题,但没有为我的问题提供解决方案

I have been searching this on internet, I have found some answers which were helpful like but they were not enough to solve my problem (e.g. Similar Problem but no solution provided for my problem)

我正在使用 JRate 插件,我在 div 中使用jQuery添加 div 。问题是,当我使用jQuery添加它并使用JRate函数时,它们无法正常工作。但他们正在工作而没有追加新的 div

I am using JRate plugin, I am adding a div inside a div using jQuery. The problem is that when I add it using jQuery and use the JRate Functions then they are not working. But they are working without appending a new div.

我知道如何让它发挥作用。我将不得不使用 $(document)但我不知道如何使用此代码。

I know how to make it work. I will have to use $(document) but I dont know how to use it with this code.

这里是HTML

<div class="jRate"></div>

这是我的Jquery

Here is my Jquery

$(".jRate").jRate({
    onSet: function(rating) {                             
        alert(rating);
    }
});

这是我的附加代码

var divjRate = "<div class='jRate'></div>";
$(divjRate).appendTo('.fb-jRate');

任何人都可以告诉我如何使用 $(文件)此处或您拥有的任何其他替代解决方案。

Can any one tell me how can I use $(document) here or any other alternative solution you have.

推荐答案

您需要先添加html元素,以便它在 DOM 中注册。然后,你可以在它上面调用 jRate

You need to append the html element first so that it is registered in the DOM. Then, you can call jRate on it

var divjRate = "<div><div class='jRate'></div></div>";

// Append new element to container of choice
$(divjRate).appendTo('.fb-jRate');

// Use plugin on new element
$('.jRate').jRate({
    onSet: function(rating) {                            
        alert(rating);
    }
});

这篇关于Jquery没有使用像Jackate这样的插件来为dom添加新添加的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆