....js的标题

试图改变形象,但遇到了一些麻烦。这是我到目前为止所拥有的。当我点击动作时,图像不会改变。


document.getElementById('action').addEventListener('click', function() {


    document.getElementById("01.jpg").src = "02.jpg"; 


    

})

<!DOCTYPE html>

<html>

    <head>

        <title></title>

        <meta charset="utf-8">

    </head>

    <body>

        <h1>Header 1 </h1>

        <img id="photo" src="01.jpg" />

        <br/>

        <br/>

        <a id="action" href="#">Click Me to Modify This Page</a>

    </body>

    </html>


慕仙森
浏览 204回答 2
2回答

ITMISS

在 document.getElementById 中使用图像中的 id 而不是图像名称。document.getElementById('action').addEventListener('click', function() {&nbsp; &nbsp; document.getElementById("photo").src = "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQGLpUYrlREGy0letrNu1q-Wl90fF8crtN7mg&usqp=CAU";&nbsp;})<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <title></title>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <h1>Header 1 </h1>&nbsp; &nbsp; <img id="photo" src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSqwJv6wgZh530ZXxBE2xeui6UtiJK2Uu-6UA&usqp=CAU" />&nbsp; &nbsp; <br/>&nbsp; &nbsp; <br/>&nbsp; &nbsp; <a id="action" href="#">Click Me to Modify This Page</a>&nbsp; </body></html>

慕尼黑8549860

你在这里有一个错误 - document.getElementById ("01.jpg")。您必须提供您的 ID - 而不是图像photo。document.getElementById('action').addEventListener('click', function() {&nbsp; &nbsp; document.getElementById("photo").src = 'https://sun9-32.userapi.com/impf/c824201/v824201969/173424/ayWCFmi538s.jpg?size=200x0&quality=90&sign=b461a01af900c4374512c2b13455c25d&ava=1';&nbsp;&nbsp; &nbsp;&nbsp;})<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <title></title>&nbsp; &nbsp; &nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <h1>Header 1 </h1>&nbsp; &nbsp; &nbsp; &nbsp; <img id="photo" src="01.jpg" />&nbsp; &nbsp; &nbsp; &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; <a id="action" href="#">Click Me to Modify This Page</a>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript