簡體   English   中英

jQuery追加后選擇右

[英]Jquery Selecting Right After appending

我的頁面上有一個按鈕,單擊該按鈕后,它會添加一個類為.select2-selection__rendered的元素。 所以我想在追加之后獲取它的文本,並且在頁面加載時不追加它的文本,當我單擊一個按鈕時追加它,之后我想在AJAX調用中使用它,但是當我通過提醒它的文本來測試它時,我一無所有,一片空白! 附加沒有問題。 它追加了span元素。 這是我選擇文本的腳本。

$(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);
          });
      });  

=============================新編輯================== =========

大家好,我不想附加任何內容,我想選擇要由腳本附加的元素的文本,並且該腳本僅在單擊myButtn(例如名稱)時運行。 而且那個腳本不是我寫的,而且太長了,我下載了它...等等,讓我給你看一個例子

參見上方圖片...。現在參見下方圖片

參見上方圖片...。現在參見下方圖片

在此處輸入圖片說明

現在有什么建議或幫助...嗎? :(

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

如果我沒看錯,您的意思是這樣的:

$(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);
  });
});

甚至

$(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);
  });
});

根據編輯后的評論,我認為這應該可行:

$(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