Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.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 通过.text()存储变量不是';最新_Javascript_Jquery - Fatal编程技术网

Javascript 通过.text()存储变量不是';最新

Javascript 通过.text()存储变量不是';最新,javascript,jquery,Javascript,Jquery,我在一个div中有标记,当用户单击这些标记时,它们会被删除。很好 我想将.text()存储在该div中的变量中。问题是,更新后的文本无法存储 单击某个单词以删除该单词 如您所见,content变量返回旧文本,而不是新修订的文本 如何存储具有更新文本的变量 jQuery: jQuery(document).ready(function() { jQuery(document).on("mousedown", ".hello span", function() { //

我在一个div中有
标记,当用户单击这些标记时,它们会被删除。很好

我想将
.text()
存储在该div中的变量中。问题是,更新后的文本无法存储

单击某个单词以删除该单词

如您所见,
content
变量返回旧文本,而不是新修订的文本

如何存储具有更新文本的变量

jQuery:

jQuery(document).ready(function() {

    jQuery(document).on("mousedown", ".hello span", function() {

        // don't add full stop at the end of sentence if it already ends with
        var endChars = [".", "?", "!"];

        jQuery(this).fadeOut(function(){
            var parentObj = jQuery(this).parent();
            jQuery(this).remove();
            var text = parentObj.find("span").first().html();
            parentObj.find("span").first().html(ta_capitalizeFirstLetter(text));
            text = parentObj.find("span").last().html();
            if ( endChars.indexOf(text.slice(-1))  == -1 )
            {
                parentObj.find("span").last().html(text+".");
            }
        });

        var content = jQuery(this).parent().parent().find('.hello').text();

        alert(content);

    });
});

我尝试在chrome中调试您的JSFIDLE,您的代码的优先级如下:

  • 在此事件上声明-
    jQuery(this).fadeOut(function(){

  • 获取div
    var content=jQuery(this.parent().parent().find('.hello').text();
    的当前数据

  • 在不进行更改的情况下提醒您的数据

  • 调用淡出函数

  • 我想你所要做的就是从淡出的匿名函数中调用alert和2。获取新文本的代码应该移动到淡出回调中。一旦动画完成并且元素被删除,那么父元素的innerText将被更新。此时,更新的内容应该被删除从DOM中读取

    下面是另一个简单的演示,其中包含少量代码,这将有助于更好地理解代码


    只需将您的警报放在回调中:

    jQuery(this).fadeOut(function(){
      var parentObj = jQuery(this).parent();
      jQuery(this).remove();
      var text = parentObj.find("span").first().html();
      parentObj.find("span").first().html(ta_capitalizeFirstLetter(text));
      text = parentObj.find("span").last().html();
      if ( endChars.indexOf(text.slice(-1))  == -1 ) {
        parentObj.find("span").last().html(text+".");
        var content = parentObj.parent().find('.hello').text();
        alert(content);
      }
    });
    

    我在试着理解这个问题。你能把每次点击都会发生什么添加到demo@Tushar这很简单。在JSFIDLE中,单击单词
    lorem
    ,然后警报框应该显示
    Hello world
    ,而不是
    Hello world lorem
    。这有意义吗?是变量
    内容
    出了问题。…我得到了这个错误-未捕获引用错误:ta_大写字母不是defined@thescion因此,忘记添加该函数了,这与此无关,请参阅更新的fiddle:@henrikpeterson“它非常简单。”我相信你不是有意的,但是尽量避免那些可能被认为是对试图帮助你的人居高临下的措辞/措辞。
    jQuery(this).fadeOut(function(){
      var parentObj = jQuery(this).parent();
      jQuery(this).remove();
      var text = parentObj.find("span").first().html();
      parentObj.find("span").first().html(ta_capitalizeFirstLetter(text));
      text = parentObj.find("span").last().html();
      if ( endChars.indexOf(text.slice(-1))  == -1 ) {
        parentObj.find("span").last().html(text+".");
        var content = parentObj.parent().find('.hello').text();
        alert(content);
      }
    });