当我单击 anchor 标记时,我试图更改 ID 为 #latestgames 的 div 的内容。此代码位于结束正文标记之前
<script type="text/javascript">
$(function(){
$("a").click(function(){
if($(this).attr("id") == "web") {
$("#latestgames").html("<div>
</div>")
}
else if($(this).attr("id") == "app") {
$("#latestgames").html("<div>
</div>")
}
else if($(this).attr("id") == "uni") {
$("#latestgames").html("<div>
</div>")
}
else if($(this).attr("id") == "other") {
$("#latestgames").html("<div>
</div>")
}
});
});
</script>
每个 div 的内容将用 ul 填充(如果相关的话)。 anchor 标签列表如下
<li class="portfolio"><a id="web" href="javascript:void(0);">Web Development</a></li>
<li class="portfolio"><a id="app" href="javascript:void(0);">Web Development</a></li>
<li class="portfolio"><a id="uni" href="javascript:void(0);">Web Development</a></li>
<li class="portfolio"><a id="other" href="javascript:void(0);">Web Development</a></li>
在头部我调用以下内容
<script type='text/javascript' src='https://ajax.googleapis.com/ajax
/libs/jquery/1.7.2/jquery.min.js'></script>
任何关于我可能做错的事情的见解将不胜感激!
最佳答案
实际上,在 javascript 中,您无法在一行上打开一个字符串并在多行后将其关闭。
你应该更换
$("#latestgames").html("<div>
</div>")
}
与
$("#latestgames").html("<div></div>")
}
关于javascript - 使用 Javascript 更改 div 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17244534/