简体   繁体   English

如何将JS文件中定义的相同规则(包含document.ready(..)中的所有规则)应用于新帖子?

[英]How to apply the same rules defined in a JS file, containing all rules within document.ready(..) to new posts?

I have a simple JS file that uses Jquery to apply rules to the loaded page. 我有一个简单的JS文件,该文件使用Jquery将规则应用于加载的页面。

I starts with the traditional $(document).ready(function(){ 我从传统的$(document).ready(function(){

Nevertheless, when I load more posts (load more button) or submit a new post, those rules don't apply. 但是,当我加载更多帖子(加载更多按钮)或提交新帖子时,这些规则将不适用。 I think I understand why...though it is not clear. 我想我明白为什么...虽然不清楚。

Is there a way to apply the same rules to each new added post? 有没有办法对每个新添加的帖子应用相同的规则? Is the only way defining events directly on the html code like eg onclick....? 是直接在html代码上定义事件的唯一方法,例如onclick ....?

I may be a very simple question. 我可能是一个非常简单的问题。 I'll appreciate any answers :) 我将不胜感激任何答案:)

Thanks 谢谢

JS Code JS代码

    $(document).ready(function(){

    (...)

    $('button#cancel').on('click',function () {

    $(this).parents('.footer').hide();
    $(this).parents('.footer').siblings('.small-textarea-main-feed').removeClass('set-large');
    $(this).parents('.footer').siblings('.small-textarea-main-feed').val('');
});


    (...)

 }); closes all

I am using the following code in load_questions.js to load a new post: 我在load_questions.js中使用以下代码来加载新帖子:

    $('form.ajax').submit(function() {
    // 
    var that = $(this),
    url = that.attr('action'),
    type = that.attr('method'),
    data = {};

    that.find('[name]').each(function(index, value) {

        var that = $(this),
        name = that.attr('name'),
        value = that.val();

        data[name] = value;

    });

    //event.preventDefault();
    $.ajax({

        url: url,
        type: type,
        data: data,
        cache: false, // it will force requested pages not to be cached by the browse
        success: function(html){
                    console.log(html);
                    $("ol#list-feed").prepend(html);
                    $("ol#list-feed li:first").slideDown(600);                          
                    document.getElementById('set-width1').value='';
                    document.getElementById('tags').value='';

                    if ($("ol#list-feed > li").size() <= 3) {
                        $('#loadmorebutton').hide();
                    } else {
                        $("ol#list-feed > li:last").remove();
                        $('#loadmorebutton').show();
                    }

                }


    });
    //event.preventDefault();
    return false;
});

I want that this type of rules apply to new posts I submit. 我希望这种规则适用于我提交的新帖子。

The DOMDocumentReady event fires exactly once on the page, when the entire HTML document has been loaded and parsed, so any DOM element you should be able to expect be on the page, will be. 当整个HTML文档已加载并解析后,DOMDocumentReady事件在页面上仅触发一次,因此,您应该可以期望的所有DOM元素都将在页面上触发。

Any DOM elements you add to the page from this point on, need to be decorated again. 从现在开始,您添加到页面的所有DOM元素都需要再次进行修饰。 This can be as simple as calling the DOMDocumentReady handler again, in which case you'd want to make it a named function, and pass that named function to $(document).ready(...) . 这可以像再次调用DOMDocumentReady处理程序一样简单,在这种情况下,您想使其成为一个命名函数,然后将该命名函数传递给$(document).ready(...) Something like this: 像这样:

var onReadyHandler = function() { };
$(document).ready(onReadyHandler);
$.ajax({
   success: function(html) {
      $("ol#list-feed").prepend(html);
      onReadyHandler();
   }
});

Now, it's likely that a better way of handling this (it's really unclear to me what precisely you're trying to accomplish, but that's not a real problem), is to not bind anything to your new posts at all. 现在,可能有更好的处理方式(对我来说真的很不清楚,您到底想完成什么,但这不是一个真正的问题),根本不将任何内容绑定到您的新帖子上。 If you're concerned about events, bind the events to the container you know will be on the page, using 'event delegation' (jQuery link: http://api.jquery.com/delegate/ ). 如果您担心事件,请使用“事件委托”(jQuery链接: http : //api.jquery.com/delegate/ )将事件绑定到您将在页面上知道的容器。 This pattern takes advantage of the fact that events 'bubble' in the DOM, meaning you can listen higher in the DOM then the elements you actually want to respond to, and just check that the click event happened on the event you do care about ($.delegate does this check automatically). 这种模式利用了事件在DOM中“冒泡”这一事实,这意味着您可以在DOM中侦听比您实际想要响应的元素更高的声音,并只需检查一下您确实关心的事件是否发生了click事件( $ .delegate自动执行此检查)。 The end result? 最终结果? You bind far fewer event handlers, since you're not decorating each post individually. 您绑定的事件处理程序少得多,因为您没有单独装饰每个帖子。

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

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