在Ajax调用之后重新触发Jquery脚本

问题描述 投票:-1回答:1

我正在使用一个简短的Jquery脚本来在页面加载时隐藏div元素,这在某些条件下会重新出现。当用户向下滚动页面时,新内容将加载Ajax,其中包含我想要隐藏的非常相同的div元素。如何在加载div元素时再次触发那个Jquery?

<head>

<script type="text/javascript">
    (function($) {
           $(window).load(function() {
            $( ".the_button" ).hide();
            if (...certain condition is met...) { 
                $( ".the_button" ).show();
            }
           });
    })(jQuery);
</script>

</head>

<body>

<div class="the_button"><button>Share</button></div>

<p>Ajax-loaded button:</p>

<div class="the_button"><button>Share</button></div>

</body>

该按钮显示在后续的Ajax加载中,但我希望它像第一个一样隐藏。

javascript jquery ajax
1个回答
1
投票

您可以将两个脚本放在“filename.js” - 文件中,并将其包含在带脚本标记的html中。

然后把你的整个脚本放在这样的函数中

$(window).load(function () {
    function functionName(){
    $(".the_button").hide()
    if (...certain condition is met...) {
      $(".the_button").show()
    }
    functionName()
      (...calling it right after load...)
    $.ajax({
        (...ajax things goes here...)
      })
      .done(function(){
        functionName()
          (...call your function again here...)
      })
    }
  })

这件事可以帮助你处理ajax。 http://api.jquery.com/jquery.ajax/

© www.soinside.com 2019 - 2024. All rights reserved.