简体   繁体   English

javascript—更改图像的innerHTML

[英]javascript— changing the innerHTML of an image

So I JUST asked a question and I've already hit another roadblock. 所以我只问了一个问题,而我已经遇到了另一个障碍。 Thanks so much to the stackoverflow community for helping me out so much recently :) 非常感谢stackoverflow社区最近对我的帮助:)

Every time I see people changing the innerHTML of some tag, they use document.getElementByID(id).innerHTML. 每当我看到人们更改某些标签的innerHTML时,他们就会使用document.getElementByID(id).innerHTML。 My images do NOT have ids, nor do I want to give each and every one of them an id. 我的图像没有ID,我也不想给每个ID提供ID。

Here is my code: 这是我的代码:

function clickImages()
{
var images = document.getElementsByTagName("img");
for(var i = 0; i < images.length; i++)
{
     var delegate = function() { hover(this); };

  images[i].onmouseover = delegate;
}
}

function hover(img)
{
img.innerHTML = "Text!";
}

The innerHTML doesn't seem like it's working; innerHTML似乎不起作用。 when I mouse over the image nothing changes. 当我将鼠标悬停在图像上时,没有任何变化。

Thanks in advance! 提前致谢! Let me know if I can clarify! 让我知道是否可以澄清!

An image is itself a replaced element, so there is no innerHTML to replace. 图像本身就是被替换的元素,因此没有要替换的innerHTML

Based on your comment, if you want to add HTML adjacent to the image, you could do this: 根据您的评论,如果要在图像旁边添加HTML,可以执行以下操作:

img.insertAdjacentHTML('afterend', 'HTML code');

You could also change 'afterend' to 'beforebegin' if you wanted to add the HTML before the image. 如果要在图像之前添加HTML,也可以将“ afterend”更改为“ beforebegin”。

Writing on the image itself would be much more complicated and would require positioning another element above it with a transparent background, or using the image itself as the background of another element, or a variety of other similar techniques. 在图像本身上书写会更加复杂,并且需要使用透明背景在其上方放置另一个元素,或者将图像本身用作另一个元素的背景,或者使用多种其他类似技术。

Element.innerHTML Element.innerHTML

innerHTML sets or gets the HTML syntax describing the element's descendants innerHTML设置或获取描述元素后代的HTML语法

innerHTML means the HTML that comes between the start and end HTML tags. innerHTML表示介于开始和结束HTML标记之间的HTML。 Images don't have innerHTML 图片没有innerHTML

I am not sure what you want to achieve. 我不确定您要实现什么。

If you want to have a text displayed onmouseover , you can use the title attribute to do that.. for example: 如果要在onmouseover显示文本,则可以使用title属性来执行此操作。例如:

<img src="smiley.gif" alt="Smiley face" title="Hello" />

If you are trying to replace the image with text on onmouseover ... then as an example (only to show the possibilities), the following code will hide the image & display a text onmouseover and make the image visible & remove the text onmouseout (although not very satisfactorily IMHO, more work has to be done on the code to improve it) 如果您尝试在onmouseover上用文本替换图像...,那么作为一个示例(仅显示可能性),以下代码将隐藏图像并在onmouseover显示文本,并使图像可见并在onmouseout删除文本(尽管恕我直言,效果不是很令人满意,但必须对代码进行更多的工作才能对其进行改进)

Please note: There shouldn't be any gap between the image tag and the span for this particular code to work. 请注意:图片标签和跨度之间不应有任何间隙,此特定代码才能正常工作。 Also, a negative margin-left added to the span element can make it appear over the location of the image. 另外,向span元素添加负的margin-left会使它显示在图像的位置上。

<img onmouseover="toText(this)" onmouseout="toImage(this)" src="smiley.gif" alt="Smiley" /><span style="visibility: hidden; margin-left: -30px;">Show Text</span>

function toText(img) {

 img.style.visibility = 'hidden';
 img.nextSibling.style.visibility = 'visible';
}

function toImage(img) {

  img.style.visibility = 'visible';
  img.nextSibling.style.visibility = 'hidden';
}

Good luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM