将对象字符串ID设置为Javascript参数

将对象字符串ID设置为Javascript参数,javascript,html,Javascript,Html,我开始使用HTML、CSS和Javascript进行编码,但我遇到了一个问题。我有一个博主,我的文章通常都有很多内容,所以我想通过分类来拆分我的文章,并使用类似于扰流板的东西。所以,我使用了一些div标签,就像上面一样 实例 举个例子 内容将放置在此分区上 我已经设置了一个默认的ID字符串,但不知道是否需要它 我想你的问题是你的参数没有被称为字符串。应该是: <div Onclick="click1('content_1', 'icon_1')"/> 你这样称呼它: <

我开始使用HTML、CSS和Javascript进行编码,但我遇到了一个问题。我有一个博主,我的文章通常都有很多内容,所以我想通过分类来拆分我的文章,并使用类似于扰流板的东西。所以,我使用了一些div标签,就像上面一样

实例

举个例子 内容将放置在此分区上

我已经设置了一个默认的ID字符串,但不知道是否需要它


我想你的问题是你的参数没有被称为字符串。应该是:

<div Onclick="click1('content_1', 'icon_1')"/>
你这样称呼它:

<div Onclick="click1('1')"/>

我创建了下面的代码片段,它提供了与您期望的相同的功能/行为,但实现方式几乎没有什么不同

希望这能解决你的问题。你可以在上面测试/播放这个


在函数图标_change中有var image=document.getElementByIdicon。这应该是ico而不是iconTrue,感谢您的注意:。但仍然不起作用很好:非常感谢JulienGrégoire。还有一个问题。如果我使用'content_1',我将为Javascript参数设置一个与ID字符串相对应的字符串值。如果我只是在参数中使用content_1,函数如何解释它?
<div Onclick="click1('1')"/>
<figure onclick="showHide('show');" id="show">
    <img src="http://bioinformatica.upf.edu/2009/projectes09/Ex/resultats/seli/SelI_human_pfam_files/showButton.png" alt="An awesome picture">  
    <figcaption>Show</figcaption>
</figure>

<figure onclick="showHide('hide');" id="hide" style="display:none;">
    <img src="http://tweetingmeeting.com/images/hide-button.png">   
    <figcaption>Hide</figcaption>
</figure>


<div id="cont_1" style="display:none;">
    <p>just some stuff for an example<br/>
        content will be placed on this div</p>
    <p>some more content.....</p>
</div>


<script>
function showHide(activity)
{
    show = document.getElementById("show");
    hide = document.getElementById("hide");
    content = document.getElementById("cont_1");

    // a.style.display = "none";
    if(activity == "show")
    {
        show.style.display = "none";
        hide.style.display = "block";
        content.style.display = "block";
    }
    else if(activity == "hide")
    {
        show.style.display = "block";
        hide.style.display = "none";
        content.style.display = "none";
    }
}
</script>