简体   繁体   English

jQuery使Rateit(star)插件适用于添加的元素

[英]Jquery making Rateit (star) plugin work for added elements

I have some rating code: 我有一些评级代码:

$('div.rateit').rateit();

I want it also to function for added elements. 我希望它也可用于添加的元素。

Here is the plugin: http://rateit.codeplex.com/ 这是插件: http : //rateit.codeplex.com/

It works! 有用! :

If you have a look at the source of the rateit plugin (src/jquery.rateit.js) you'll see that at the end of file appears the invocation: 如果您查看Rateit插件的源代码(src / jquery.rateit.js),您会看到在文件末尾出现了调用:

//invoke it on all .rateit elements. This could be removed if not wanted.
$(function () { $('div.rateit, span.rateit').rateit(); });

What you need to do is to invoke this function again once you load or add elements. 您需要做的是在加载或添加元素后再次调用此函数。

In my case I was using jQuery.ajax (...) and when loading new elements with the 'rateit' css class it wasn't working. 在我的情况下,我使用的是jQuery.ajax(...),并且在使用“ rateit” css类加载新元素时不起作用。 Now they do with this line inside the ajax response: 现在,他们在ajax响应中使用以下行:

<script type="text/javascript">          
jQuery(document).ready(function(){
    jQuery('div.rateit, span.rateit').rateit(); 
});
</script>

Since you are adding the new elements server-side, just add the CSS class rateit to each element you are adding. 由于要在服务器端添加新元素,因此只需将CSS类rateit添加到要添加的每个元素。 Assuming you only want to make the div elements a rateit object, your existing jQuery code will work. 假设您只想使div元素成为rateit对象,那么您现有的jQuery代码将起作用。

似乎您可能需要使用jQuery on()方法 ,如下所示:

  $('div.rateit').on('event which triggers the rateit()', rateit);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM