Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更新div内容后使函数工作_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 更新div内容后使函数工作

Javascript 更新div内容后使函数工作,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一个有内容的div。当页面加载时,此函数工作正常。但是,当我使用javascript用内容更新div时,该函数无法工作 有人能帮我弄清楚我该怎么做才能让这一切顺利进行吗 $("textarea.indent").each(function(){ var indentWidth = $(this).siblings('[class=indent]').width(); if(indentWidth != null) $(this).css('text-indent',

我有一个有内容的div。当页面加载时,此函数工作正常。但是,当我使用javascript用内容更新div时,该函数无法工作

有人能帮我弄清楚我该怎么做才能让这一切顺利进行吗

$("textarea.indent").each(function(){
    var indentWidth = $(this).siblings('[class=indent]').width();
    if(indentWidth != null)
    $(this).css('text-indent', (indentWidth+5)+'px');
});
您正在动态加载$(“textarea.indent”)吗

Jquery在页面加载时绑定文档就绪上的所有事件。这意味着页面加载完成后添加的元素不会绑定到该事件。要解决此问题,还需要动态绑定事件。像这样

$.ajax{
   ...
   //Some ajax call
   success: function(){
         //Bind event
         $("textarea.indent").each(function(){
              var indentWidth = $(this).siblings('[class=indent]').width();
              if(indentWidth != null)
              $(this).css('text-indent', (indentWidth+5)+'px');
         });
   }
}

添加元素不一定是ajax请求,但我的观点仍然站得住脚

我想这就是问题所在。