尝试使用 JS 使 HTML 中的 img 可点击

我希望我的 HTML 中的文本区域仅通过单击我的 HTML 中的 img 出现。我正在尝试找出一种使用 JS 中的 onclick 事件的方法;但我不太清楚该怎么做。有人对我的方法有什么建议吗?任何帮助将不胜感激。谢谢,麻烦您了。


<script>

function myFunction() {

  document.getElementById("itg").innerHTML = ;

}

</script>

<style>

  #itg{

    height:150px;

    width:150px;

    border-radius:50%;

    align:top;

  }

    body{

        background-image:url("codercup.png"),linear-gradient(to right,white,#c3c3c3);

        background-repeat: no-repeat;

        background-size: 600px, 700px;

        background-position:bottom,center;  

    }

    

    li{

        list-style-type:none;

        padding:0;

        margin:0;

        font-size:20px;

    }

    

    h1{

        text-align:center;

    }

    

    nav{

        float:right;

        height:500px;

        

    }

    

    

    .resume{

        align:bottom-left;

    }


  </style>


繁星coding
浏览 168回答 1
1回答

红颜莎娜

将文本从文本区域移动到 JavaScript 函数将 an 添加id到文本区域,以便您可以定位它将此 id 放入您的getElementById函数中function myFunction() {&nbsp; const textArea = document.createElement("textarea");&nbsp; textArea.rows = 10&nbsp; textArea.id = "text";&nbsp; textArea.cols = 30;&nbsp; textArea.readonly = true;&nbsp; textArea.innerHTML = `I have been learning and creating web page content since 2015.&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; I'm a part-time student in Information Technology with a concentration in web development, also a self taught developer.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; I have freelance experience creating multiple different projects (mostly front-end).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; I'm inspired of programming from the constant growth in technology.&nbsp; I enjoy creating things as I have always had an artistic mind; so mixing the passion of creativity, with my love for tech programming feels perfect for me.`&nbsp; const nav = document.getElementsByTagName("nav")[0];&nbsp; const header = document.getElementsByTagName("header")[0];&nbsp; header.insertBefore(textArea, nav);}<style>#itg {&nbsp; height: 150px;&nbsp; width: 150px;&nbsp; border-radius: 50%;&nbsp; align: top;}body {&nbsp; background-image: url("codercup.png"), linear-gradient(to right, white, #c3c3c3);&nbsp; background-repeat: no-repeat;&nbsp; background-size: 600px, 700px;&nbsp; background-position: bottom, center;}li {&nbsp; list-style-type: none;&nbsp; padding: 0;&nbsp; margin: 0;&nbsp; font-size: 20px;}h1 {&nbsp; text-align: center;}nav {&nbsp; float: right;&nbsp; height: 500px;}.resume {&nbsp; align: bottom-left;}</style><!DOCTYPE html><html><head>&nbsp; <meta charset="UTF-8">&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; <meta http-equiv="X-UA-Compatible" content="ie=edge">&nbsp; <title>Evin McReynolds Portfolio</title></head><body>&nbsp; <header>&nbsp; &nbsp; <h1><strong>About Evin</strong></h1>&nbsp; &nbsp; <img src="ITguy.jpeg" id="itg" onclick="myFunction() " />&nbsp; &nbsp; <nav>&nbsp; &nbsp; &nbsp; <ul class="link ">&nbsp; &nbsp; &nbsp; &nbsp; <li><a href="EMport.html ">Home</li></br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>About Evin</li></br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><a href="contactem.html ">Contact Evin</a></li>&nbsp; &nbsp; &nbsp; &nbsp; </br>&nbsp; &nbsp; &nbsp; &nbsp; <li><a href="skillsem.html ">Skills</a></li>&nbsp; &nbsp; &nbsp; &nbsp; </br>&nbsp; &nbsp; &nbsp; &nbsp; <li><a href="EvinPro.html ">Projects</a></li>&nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; </nav>&nbsp; &nbsp; </p>&nbsp; </header>&nbsp; <section>&nbsp; &nbsp; <embed src="evinITresume.pdf " width="350px " height="400px " class="resume " />&nbsp; </section></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript