可以使用 javascript 轻松更改指定给特定元素的文本和 img 元素中指定的图像。我们可以将 onclick 事件与 html 文档中的按钮元素一起使用,以便在单击按钮时发生这种情况。要更改按钮单击时的文本,我们将一个函数分配给 onclick 事件作为执行所需更改的值。
让我们通过代码示例详细了解使用 javascript 分别更改文本和图像的方法。
更改元素的文本javascript 为我们提供了两个不同的属性来更改或获取 html 文档中元素的文本,下面列出了这两个属性及其功能和语法 -
innertext - javascript 的innertext 属性用于更改先前的文本或从html 文档中获取特定选定元素的先前文本。
语法以下语法将向您展示如何使用innertext属性来获取和更改元素的文本 -
selected_element.innertext = new text ;
innerhtml -innerhtml 属性不仅提供元素的文本及其内部使用的所有子标签,而且还可以更改元素的文本及其内部使用的子标签新文本。
语法以下语法将向您展示如何使用innerhtml属性来获取或更改元素的文本 -
selected_element.innerhtml = new text ;
让我们通过代码示例中的实际实现来理解这两个属性 -
算法第 1 步 - 在第一步中,我们将输入元素添加到 html 文档中。这样,我们就可以用用户输入的文本更改以下段落的文本。
步骤 2 - 在此步骤中,我们将添加一个带有与其关联的 onclick 事件的按钮标签,该标签将一个函数作为其值,并在用户单击按钮并更改时调用它段落的文本。
第 3 步 - 在下一步中,我们将定义一个 javascript 函数,在该函数中我们将抓取用户输入的输入文本,并使用innertext 和innerhtml 属性来更改文本页面上下面的段落。
示例下面的示例将向您解释如何使用 innertext 和 innerhtml 属性来更改元素的文本 -
<html lang = en><body> <h2>changing an text of an element in the html document using javascript.</h2> <p id = upper>the text of the below element will be replaced by the text you enter in input bar once you click the button.</p> <input type = text id = inp> <br> <br> <button id = btn onclick = changeimage()> click to change the text </button> <p id = para1>this is the initial text of para1.</p> <p id = para2>this is the initial text of para2.</p> <script> var para1 = document.getelementbyid(para1); var para2 = document.getelementbyid(para2); function changeimage() { var inp = document.getelementbyid(inp); var enteredtext = inp.value; para1.innertext = enteredtext + , this text is changed using the innertext property. ; para2.innerhtml = <u> + enteredtext + </u> + , <b> this text is changed using the <em> innerhtml </em> property. <b> <br> ; } </script></body></html>
在上面的示例中,我们使用innertext 和innerhtml 属性更改了两个不同段落的文本。前一个的文本是使用innertext 属性更改的。同时,后一个文本的文本是使用innerhtml 属性更改的。
单击按钮时更改图像我们已经讨论了如何使用 javascript 更改 html 文档中元素的文本。不,我们将讨论如何仅通过使用 javascript 单击按钮来更改图像。
javascript 允许我们使用 src 属性来更改以及获取给定链接的值或给定 src 属性中的 img 元素的图像地址。
语法以下语法将展示如何使用src属性来更改网页上的图像 -
selected_img_element.src = new link or address ;
现在让我们借助 javascript 代码示例来了解 src 属性更改图像的实际实现 -
算法step 1 - 在第一步中,我们将在 html 文档中添加一个 img 元素,其 src 属性包含一些初始值,稍后我们将使用 javascript 借助 src 属性更改该值。
第 2 步 - 在下一步中,我们将添加一个带有 onclick 事件的按钮元素,该事件将在单击按钮时调用一个函数。
第 3 步 - 在这一步中,我们将定义 javascript 函数并通过其 id 获取其中的 img 元素。
步骤 4 - 在最后一步中,我们将使用 src 属性更改 src 属性的值,并为其指定一个新值以在网页上显示一些新图像。每次单击该按钮时,用户都会在每次单击时在两个图像之间切换。
示例下面的示例将解释 src 属性如何使用新值来替换 src 属性的先前值以及网页上的先前图像 -
<html><body> <h2>changing an image in the html document using javascript</h2> <p id = upper>the image shown below will be changed once you click the button.</p> <img src =https://encrypted-tbn0.gstatic.com/images?q=tbn:and9gcr1gyk6fichcrcizxh_dxsfba5idw7xaykizq&usqp=cau id = image> <br> <br> <button id = btn onclick = changeimage()> click to change the image </button> <p id = result> </p> <script> var result = document.getelementbyid(result); var upper = document.getelementbyid(upper); function changeimage() { var image = document.getelementbyid(image); if (image.src == https://encrypted-tbn0.gstatic.com/images?q=tbn:and9gcr1gyk6fichcrcizxh_dxsfba5idw7xaykizq&usqp=cau) { image.src = https://encrypted-tbn0.gstatic.com/images?q=tbn:and9gcsolnvrntnp2rojd7e9b_ilw5zzkslpotspia&usqp=cau; result.innerhtml += the image is changed from <b> light mode to dark mode </b>. <br> ; } else { image.src = https://encrypted-tbn0.gstatic.com/images?q=tbn:and9gcr1gyk6fichcrcizxh_dxsfba5idw7xaykizq&usqp=cau; result.innerhtml += the image is changed from <b> dark mode to light mode </b>. ; } upper.innerhtml = the previous image is replaced by the new image as you click the button. <br> ; } </script></body></html>
在上面的示例中,我们使用 src 属性来更改 img 元素的 src 属性值和网页上的实际图像。
在本文中,我们通过代码示例详细了解了使用 javascript 更改网页上元素文本的两种不同方法以及更改网页上图像的方法他们每一个人。这些示例将帮助您增强 javascript 的实践知识。
以上就是如何通过单击 javascript 中的按钮来更改文本和图像?的详细内容。