如何将多个 onClick 事件链接到 HTML 中的一个元素?我可以让他们更改 href 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10620052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:28:09  来源:igfitidea点击:

How can I have multiple onClick events linked to one element in HTML? And can I have them change href's?

htmlonclickmouseeventhrefreplace

提问by Henry Edward Quinn IV

I'm building a page for a company I'm interning for, and can't seem to figure this one out. The code you see below is just a simplified template, so that I don't give any information about the company away (and all the style attributes are in a separate document, so everything in the code is alright.

我正在为我实习的公司建立一个页面,但似乎无法弄清楚这一点。您在下面看到的代码只是一个简化的模板,因此我不会提供有关公司的任何信息(并且所有样式属性都在一个单独的文档中,因此代码中的所有内容都可以。

What I'm having a problem with is making multiple onClick events work. For that matter, is using the onClicks in the comment tags even feasible? I want the text that says Further Instruction to have it's href path changed depending on the animal picture that's clicked.

我遇到的问题是使多个 onClick 事件起作用。就此而言,在评论标签中使用 onClicks 是否可行?我希望显示进一步说明的文本根据单击的动物图片更改其 href 路径。

Also, I can't use any JavaScript, so please don't suggest the use of any. Happen to have any ideas?

另外,我不能使用任何 JavaScript,所以请不要建议使用任何 JavaScript。碰巧有什么想法?

<div id="container">
    <div class="mainPicture">
        <img alt="" id="awesome" name="Awesome2" src="awesome.png" />
    </div>
    <div id="gallery">
        <ul class="popOut">
            <li>
                <a href="DogText.html" target="AnimalInfo" onClick="document.Awesome2.src='dog.jpg'"> <!--onClick="document.furtherInstructions.href='DogText.html'">--> 
                    <img src="dog.jpg" alt="dog"><img src="dog.jpg" alt="dog" class="preview"> 
                </a>
            </li>   
            <li>
                <a href="CatText.html" target="AnimalInfo" onClick="document.Awesome2.src='cat.jpg'"> <!--onClick="document.furtherInstructions.href='CatText.html'">-->
                    <img src="cat.jpg" alt="cat"><img src="cat.jpg" alt="cat" class="preview"> 
                </a>
            </li>   
            <li>
                <a href="ParrotText.html" target="AnimalInfo" onClick="document.Awesome2.src='parrot.jpg'"> <!--onClick="document.furtherInstructions.href='ParrotText.html'">-->
                    <img src="parrot.jpg" alt="parrot"><img src="parrot.jpg" alt="parrot" class="preview"> 
                </a>
            </li>       
            <li>
                <a href="LizardText.html" target="AnimalInfo" onClick="document.Awesome2.src='lizard.jpg'"> <!--onClick="document.furtherInstructions.href='LizardText.html'">--> 
                    <img src="lizard.jpg" alt="lizard"><img src="lizard.jpg" alt="lizard" class="preview"> 
                </a>
            </li>       
            <li>
                <a href="HorseText.html" target="AnimalInfo" onClick="document.Awesome2.src='horse.jpg'"> <!--onClick="document.furtherInstructions.href='HorseText.html'">-->
                    <img src="horse.jpg" alt="horse"><img src="horse.jpg" alt="horse" class="preview"> 
                </a>
            </li>       
            <li>
                <a href="ChickenText.html" target="AnimalInfo" onClick="document.Awesome2.src='chicken.jpg'"> <!--onClick="document.furtherInstructions.href='ChickenText.html'">--> 
                    <img src="chicken.jpg" alt="chicken"><img src="chicken.jpg" alt="chicken" class="preview"> 
                </a>
            </li>   
            <li>
                <a href="DefaultText.html" target="AnimalInfo" class="center" onClick="document.Awesome2.src='awesome.png'"> <!--onClick="document.furtherInstructions.href='DefaultText.html'">-->
                    <p>Default Image</p>
            </a>
            </li>
        </ul>
        <div id="rightcol">
            <iframe src="DefaultText.html" title="Information About Animals" name="AnimalInfo" class="fixThisHeight">
            </iframe>
        </div>
        <div id="externalInstructions">
            <a href="DefaultText.html" name="furtherInstructions">
                <p>Further Instructions</p>
            </a>
        </div>
    </div>
</div>

回答by John Conde

Just separate your commands with a semi-colon:

只需用分号分隔您的命令:

onClick="document.Awesome2.src='chicken.jpg'; document.furtherInstructions.href='ChickenText.html';">