为什么 jQuery 不影响 Ajax 调用后生成的内容

发布于 2024-09-29 07:16:08 字数 1622 浏览 1 评论 0原文

我试图隐藏 div 中的所有 li,该 div 是在用户单击按钮时动态生成的。 div 的内容通过 Ajax 调用提取,然后分配给页面上的 div。

但出于某种原因,任何使用 jQuery 改变样式的尝试都不会产生任何影响。我注意到在尝试触发点击时还必须应用 $(selector).live('click, callback)

$.ajax({
 url: "admin.php",
 cache: false,
 data: ({
  'module':'data',
  'action':'actionName',
  'clientname':formatted
 }),
 success: function(data) {
  $('#outputDiv').html(data);
 },
 error: function(XMLHttpRequest, textStatus, errorThrown) {
  if(textStatus == "error") {
   $('#outputDiv').append("Sorry, but there was an error");
  }
 }
});

The 'data' variable returned to the success method is a nested list set e.g. <ul id='tree'><il><a></a></li></ul> etc

What I'm trying to do after the load is hide all children in this case I'd call $(' #tree > li').hide(); A trigger will then occur when the use clicks the anchor tag with:

$('a.viewdetails').click(function() {
$(this).next('ul').toggle();
});

编辑 1

所以现在我已经实现了下面提到的解决方案,但我无法让负载正常运行,它似乎没有发送 url 参数,格式是在上面生成的,可以由空格等组成:

var containerDiv = $('div#clientBusinessUnits'),
    liClickHandler = function(e) {
        $('a.viewdetails').click(function() {
            alert('1');
        });
    },
    loadCompleteHandler = function(responseText, textStatus, XMLHttpRequest) {
        console.log(responseText);
        console.log(textStatus);
        $('li', containerDiv).click(liClickHandler);
    };
    containerDiv.load("admin.php?module=data&action&getData&clientname="+formatted, loadCompleteHandler);

I'm attempting to hide all li's within a div which is dynamically generated when a use clicks a button. The content of the div is pulled through an Ajax call and I then assign to a div on the page.

For some reason though, any attempt to alter the style with jQuery doesn't have any affect. I notice I also have to apply $(selector).live('click, callback) when attempting to trigger a click.

$.ajax({
 url: "admin.php",
 cache: false,
 data: ({
  'module':'data',
  'action':'actionName',
  'clientname':formatted
 }),
 success: function(data) {
  $('#outputDiv').html(data);
 },
 error: function(XMLHttpRequest, textStatus, errorThrown) {
  if(textStatus == "error") {
   $('#outputDiv').append("Sorry, but there was an error");
  }
 }
});

The 'data' variable returned to the success method is a nested list set e.g. <ul id='tree'><il><a></a></li></ul> etc

What I'm trying to do after the load is hide all children in this case I'd call $(' #tree > li').hide(); A trigger will then occur when the use clicks the anchor tag with:

$('a.viewdetails').click(function() {
$(this).next('ul').toggle();
});

Edit 1

So now I've implemented the solution mentioned below but I can't get the load to function correctly, it doesn't appear to send the url parameters, formatted is generated above and can consist of spaces etc:

var containerDiv = $('div#clientBusinessUnits'),
    liClickHandler = function(e) {
        $('a.viewdetails').click(function() {
            alert('1');
        });
    },
    loadCompleteHandler = function(responseText, textStatus, XMLHttpRequest) {
        console.log(responseText);
        console.log(textStatus);
        $('li', containerDiv).click(liClickHandler);
    };
    containerDiv.load("admin.php?module=data&action&getData&clientname="+formatted, loadCompleteHandler);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦罢 2024-10-06 07:16:08

任何绑定到特定元素的事件处理程序(在元素存在于页面上之前)都将不起作用。

.live() 方法对此有所帮助,因为它可以让您提前将事件处理程序绑定到动态插入元素后与选择器匹配的元素。

因此,在不知道您的实际代码的情况下,您的问题的解决方案是,要么

  1. 因此使用 .live() 进行事件绑定(可能意味着深层嵌套页面上的性能问题),
  2. 要么在之后绑定您的事件处理程序> 您已经动态创建了元素(即在 ajax 方法的回调函数中绑定处理程序)。

粗略地描绘了示例:

var
  $containerDiv = $('div#container'),
  liClickHandler =
    function(e)
    {
      // hide your li element, or whatever you want to do on click events
    },
  loadCompleteHandler =
    function(responseText, textStatus, XMLHttpRequest)
    {
      $('li', $containerDiv).click(liClickHandler);
    };

$containerDiv
  .load(
    urlToPhpScript,
    loadCompleteHandler
  );

Any event handlers bound to specific elements - before the elements exist on the page - won't work.

The .live() method helps with that, in that it will let you bind event handlers in advance, to elements that match a selector after elements have been dynamically inserted.

So, without knowing your actual code, a solution for your problem would be to, either

  1. Consequently use .live() for your event binding (might imply performance issues on deeply nested pages),
  2. Bind your event handlers after you have created your elements dynamically (i.e. bind the handlers in a callback function of your ajax method).

Roughly sketched example:

var
  $containerDiv = $('div#container'),
  liClickHandler =
    function(e)
    {
      // hide your li element, or whatever you want to do on click events
    },
  loadCompleteHandler =
    function(responseText, textStatus, XMLHttpRequest)
    {
      $('li', $containerDiv).click(liClickHandler);
    };

$containerDiv
  .load(
    urlToPhpScript,
    loadCompleteHandler
  );
尸血腥色 2024-10-06 07:16:08

这是因为代码仅在脚本加载后运行一次。如果您通过 ajax 调用更改 html,则需要重新运行代码以隐藏 li。要实现此目的,您可以将代码放入一个函数中,并在站点加载后调用此函数,并且在站点更新后,如果您从 ajax 调用的回调函数中调用此函数,则可以执行此操作。

It is because the Code is only run once after the script is loaded. If you change you html via an ajax call you need to rerun the code to hide the lis. To accomplish this you can put the code into a function and call this function once the site is loaded and once the site gets updated, you can do this if you call this function from within the callback function of the ajax call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文