javascript - 使用 Javascript 更改 div 的内容

标签 javascript jquery html

当我单击 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/

相关文章:

javascript - 单击链接时,查找带有某个子项的父项是否存在jquery

javascript - WordPress 窗口滚动功能不起作用

javascript - 如何在 javascript 中没有键的情况下对 forEach 结果进行分组?

javascript - 删除 chart.js 中的 x 轴标签/文本

php - 图像是否已加载并保持隐藏状态,或者它们是否未加载到隐藏的 div 中?

html - 如何在比例动画中将原点保持在图像的中心?

javascript - 加税 - Paypal 变量

javascript - jQuery 错误 = 语法错误 : missing : after property id

javascript - Canvas 乒乓球闪闪发光

javascript - (function)(variable) 在 javascript 中是什么意思?