Html 为什么我们不能为 <img> 标签添加 id 属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12695588/
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
Why Can't we add id attribute to <img> tag?
提问by Rohitink
Whenever We add id
attribute to the <img>
tag, the image does not show up.
每当我们id
向<img>
标签添加属性时,图像都不会显示。
for eg.
例如。
<img src="something.jpg" id="sidebarAd">
<img src="something.jpg" id="sidebarAd">
This Image will not show up. Also, If I Place in it a <div>
as well. It will not work. For E.g.
此图像不会显示。另外,如果我也把它放在里面<div>
。不起作用。例如
<div id="sidebarAd">
<img src="something.jpg">
</div>
But, if i use a class tag. Everything works like a charm. Why does this happen ?
但是,如果我使用类标签。一切都像一个魅力。为什么会发生这种情况?
回答by Madara's Ghost
This is probably caused by an AdBlocker filter responding to your ID name #sidebarAd
. Which is expected behavior because... Well... It's an ad :)
这可能是由 AdBlocker 过滤器响应您的 ID 名称引起的#sidebarAd
。这是预期的行为,因为......嗯......这是一个广告:)
This demo by @zzzzBovdemonstrates that when my AdBlock Chrome extension is active, the image is invisible, but when disabled, the image is shown.
@zzzzBov 的这个演示演示了当我的 AdBlock Chrome 扩展程序处于活动状态时,图像是不可见的,但是当禁用时,图像会显示出来。
回答by Sabe
Make sure that you're using unique ID's, and a closing tag.
确保您使用的是唯一 ID 和结束标记。
回答by Gabriele Petrioli
Most likely you have a css rule somewhere about that id #sidebarAd
that sets the element that has it to be invisible..
很可能您在某个 id 的某个地方有一个 css 规则,该规则#sidebarAd
将具有它的元素设置为不可见。
display:none
for example
display:none
例如
回答by Erty Seidohl
Open your developer console (f12 in most browsers) and inspect the element to see if there is some CSS Rule.
打开您的开发者控制台(在大多数浏览器中为 f12)并检查元素以查看是否存在某些 CSS 规则。
Another idea: Since you have the word "ad" in the id, make sure that you don't have an adblock extension enabled.
另一个想法:由于您的 id 中有“ad”一词,请确保您没有启用广告拦截扩展。
回答by Mike
Do you have any css associated with that ID that may be hiding it? Any HTML element can have an ID associated with it, but all ID's must be unique (they can only occur once on each page).
您是否有任何与该 ID 关联的 css 可能隐藏它?任何 HTML 元素都可以有一个关联的 ID,但所有 ID 必须是唯一的(它们只能在每个页面上出现一次)。