我意识到这是一个常见的问题,我已经搜索了解决方案和我正在尝试使用的解决方案(它在jsfiddle中工作)但是当我尝试在我的网站中使用它时它就无法工作了。我正试图在点击按钮时显示div。难道我做错了什么?也许javascript有问题?或者我可能需要包含一个jquery js文件?感谢您的帮助。
以下是我正在尝试使用的代码:
<script type="text/javascript">
$("#seeAnswer").click(function() {
$('#subtext').html($(this).next().html());
});
</script>
我正试图用它:
<div class="back_button">
<button id="seeAnswer">See Answer</button>
</div>
<span>
<?php
/* foreach($row2 as $ans) {
$answer = $ans['answer'];
}
echo $answer;
echo "hi";
*/
?>
<?php foreach ($row2 as $ans) : ?>
<p><?php htmlspecialchars($ans['answer']) ?></p>
<?php endforeach ?>
<p>hi there</p>
</span>
<div id="subtext">
</div>
答案 0 :(得分:1)
在.parent()
之后使用.next()
,因为你指的是按钮后的下一个元素......但是没有元素...所以你想要父元素的next()
编辑:根据有关<HEAD>
的新信息,您在整个dom准备就绪之前执行代码...所以您实际上是将代码应用于任何内容。您需要等待DOM准备好。喜欢这个
$('docment').ready(function() {
$("#seeAnswer").click(function () {
$('#subtext').html($(this).parent().next().html());
});
});