Jquery making Rateit (star) plugin work for added elements

半世苍凉 提交于 2019-12-25 06:57:12

问题


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/


回答1:


Seems you might need to use jQuery on() method, like this:

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



回答2:


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:

//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. Now they do with this line inside the ajax response:

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



回答3:


Since you are adding the new elements server-side, just add the CSS class rateit to each element you are adding. Assuming you only want to make the div elements a rateit object, your existing jQuery code will work.



来源:https://stackoverflow.com/questions/9474083/jquery-making-rateit-star-plugin-work-for-added-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!