简体   繁体   English

jQuery追加后选择右

[英]Jquery Selecting Right After appending

There is a button in my page and after clicking on it, it append some element with class .select2-selection__rendered . 我的页面上有一个按钮,单击该按钮后,它会添加一个类为.select2-selection__rendered的元素。 So i want to get its text after appending and it don't appends when the page loads, it appends when i click on a button and after that i want to use it in AJAX call but when i was testing it by alerting it's text i got nothing, its blank! 所以我想在追加之后获取它的文本,并且在页面加载时不追加它的文本,当我单击一个按钮时追加它,之后我想在AJAX调用中使用它,但是当我通过提醒它的文本来测试它时,我一无所有,一片空白! . There is not a problem with appending. 附加没有问题。 It appends it's span element. 它追加了span元素。 Here is my script of selecting it's text. 这是我选择文本的脚本。

$(document).ready(function(){  
          function () {
        var postTitle = $(".select2-selection__rendered").text(); //selecting text
        $("div#sdf").click(function(){ //this div is like a button
              alert("Title is: " + postTitle);
          });
      });  

==============================NEW EDITED=========================== =============================新编辑================== =========

Hey guys, i don't want to append something, i want to select the text of element that is being appended by script and that script only run when i click myButtn(name as example). 大家好,我不想附加任何内容,我想选择要由脚本附加的元素的文本,并且该脚本仅在单击myButtn(例如名称)时运行。 And that script is not written by me and it's too long, i downloaded it...wait let me show you an example 而且那个脚本不是我写的,而且太长了,我下载了它...等等,让我给你看一个例子

参见上方图片...。现在参见下方图片

See the Above image....Now see this below image 参见上方图片...。现在参见下方图片

在此处输入图片说明

Now any suggestions or help....? 现在有什么建议或帮助...吗? :( :(

==============================EDITED================================= ============================== EDITED =================== ==============

If I understood right, you mean something like this : 如果我没看错,您的意思是这样的:

$(document).ready(function(){  
  $("div#sdf").click(function(){
    var select2 = $(".select2-selection__rendered").appendTo('body'); // replace 'body' by the DOM element to append to
    var postTitle = select2.text();
    alert("Title is: " + postTitle);
  });
});

or even 甚至

$(document).ready(function(){  
  $("div#sdf").click(function(){
    var postTitle = $(".select2-selection__rendered").appendTo('body').text(); // replace 'body' by the DOM element to append to
    alert("Title is: " + postTitle);
  });
});

based on edited comment I think this should work : 根据编辑后的评论,我认为这应该可行:

$(document).ready(function(){  
  $("div#sdf").click(function(){
    var postTitle = $(".select2-selection__rendered").text();
    alert("Title is: " + postTitle);
  });
});
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

<script>

    $(document).ready(function(){  

            var postTitle = $(".select2-selection__rendered").text(); //selecting text
            $("div#sdf").click(function () { //this div is like a button
                $(".select2-selection__rendered").append("<span style='color:red;'>hello</span>")
            });
        });  
</script>
</head>
<body>
   <div class="select2-selection__rendered" >
        sample Text
    </div>
    <br />
    <div id="sdf">Click Me</div>
</body>
</html>

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

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