2016-02-27 35 views
-4

不工作我使用jQuery函數的.text()和.html()jQuery中

 $("#btn1").click(function(){ 
      $("p").text("") 
     } 

每當我點擊.text()字段沒有加載按鈕,當我更新.text值到小弦它的工作原理。

+2

它不清楚笏你是問。你可以請顯示代碼片段或分享它在小提琴 – Vatsal

+1

你在想什麼?試圖獲取文本或設置文本? –

+0

'.text(「」)'將清除'p'中的文本。你想做什麼。 – Justinas

回答

1
$("#btn1").click(function() 
    { 
      var mytext=$("p").html(); // for getting 
      var mytext=$("p").html("mytext value"); // for setting 

    }); 

你要,讓右括號「);」

0

嘗試this.this將顯示警報文本,當你點擊你的button.just複製,粘貼和嘗試it.hope這將幫助你

<html> 
 
<head> 
 
    <title></title> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
 
</head> 
 

 
<body> 
 
    <p>Govinda</p><br/> 
 
    <button id="click">show text</button> 
 

 
    <br/><br/><br/><br/><br/> 
 
    <h2></h2> 
 
    <button id="addtext">Add text</button> 
 

 
    
 
</body> 
 

 
<script type="text/javascript"> 
 
    $(document).ready(function(){ 
 
    $("#click").click(function(){ 
 
    \t var pText = $("p").text(); 
 
     alert("this is the paragraph text :"+ pText); 
 
    }); 
 

 
    $("#addtext").click(function(){ 
 
    \t var addT = "Added Text"; 
 
     $("h2").text(addT); 
 
    }); 
 

 

 
    
 
    }); 
 
</script> 
 

 

 
</html>

+0

謝謝你的幫助。不幸的是,我想添加大於70的字符串,所以當我點擊按鈕添加文本所需的字符串不會顯示出來。通過我的方式試圖添加lorem ipsum虛擬文本。再次感謝你! –

相關問題