[英]Get div content from a child div onclick
我有一个包装div1,其中div2包含一个字符串。 当我单击div1时,我希望div2中的内容出现在alert()中; 如何才能做到这一点 ?
document.getElementById(feats).addEventListener("click",function show() {
var htmlstring = this.innerHTML;
alert(htmlstring);
上面的代码只需添加一个均匀的侦听器,并警告包装程序中的所有html代码。
谢谢
使用jQuery ,值得学习:
$('#div1').click(function() {
alert($('#div2').text());
});
这就是您想要的:
<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>
另外,轻柔地推动,如果您开始接受答案,您将获得更多的答复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.