2010-11-24 73 views
0

我有一個包裝div1,其中div2包含一個字符串。當我點擊div1我想從div2的內容出現在alert();如何才能做到這一點 ?從小孩div獲取div內容onclick

document.getElementById(feats).addEventListener("click",function show() { 
      var htmlstring = this.innerHTML; 

      alert(htmlstring); 

上面的代碼只是簡單地添加一個偶數監聽器,並提醒所有包裝器的html代碼。

感謝

回答

0

隨着jQuery,這是非常值得學習的:

$('#div1').click(function() { 
    alert($('#div2').text()); 
}); 
+0

我還沒有dv'd你,但我不同意。爲這麼簡單的作業加載一個沉重的庫是完全沒有必要的。 – 2010-11-24 18:26:30

0

這你想要做什麼:

<html> 
<head></head> 
<body> 

<div id="div1" onclick="doAlert();" style="cursor: pointer; cursor: hand;"> 
<div id="div2"> 
    Hello, this is the contents of div2 
</div> 
</div> 

<script type="text/javascript"> 
    function doAlert() { 
    alert(document.getElementById("div2").innerHTML); 
    } 
</script> 

</body> 
</html> 

而且,溫柔輕推,你會得到你更多的答覆開始接受答案。