修改 DOM 元素的属性
这段代码缺少一些东西,但我不确定是什么。我正在用萤火虫测试它。
<script type="text/javascript">
var xArray = document.getElementsByTagName("img");
var xItem;
for (var i=0; i<xArray.length; i++){
xItem = xArray[i];
xItem.removeAttribute("width");
xItem.removeAttribute("height");
}
</script>
它被放置在我的 html 的末尾。 (我打算将其放入 iOS 的 UIWebView 中,因此没有 head 或 body 元素,但它在我正在测试的浏览器中似乎工作正常。)
在我的测试用例中,有两个图像,但通常,图像的数量是任意的,所以我正在处理带有图像标签的元素数组。
我可以在监视窗格中看到宽度和高度属性已从 javascript 对象中删除,但 html 中的实际元素并未更新。所以我相信 xItem 可能是一个副本而不是指针。我需要做什么来改变真实的 DOM 元素?
更新
感谢您的快速反馈。
这是更多信息。这是我在另一篇文章中提出的问题的延伸。原始 html 有时会包含对于容器来说太大的图像,在本例中容器宽度仅为 300 像素。这是图像太大的情况:
<style type=\"text/css\">
div{width:300px;}
p{max-width:300px;}
img {max-width:300px;}
</style>
<div style="word-wrap:break-word width:300px">
<P CLASS="Body" STYLE="text-align: center;">
<FONT SIZE="2">
<IMG TITLE="INITIATION GRAPHIC" SRC="http://www.deshow.net/d/file/travel/2009-09/arizona-grand-canyon-703-12.jpg" ALT="SUNDOOR INITIATION GRAPHIC" WIDTH="100%" HEIGHT="100%">
</FONT>
</P>
<P CLASS="Body">
<FONT SIZE="3">
<IMG TITLE="Banner" SRC="http://brown09.wikis.birmingham.k12.mi.us/file/view/Bryce_Canyon.jpg/135893343/Bryce_Canyon.jpg" ALT="Banner" WIDTH="592" HEIGHT="138">
</FONT>
</P>
</div>
<script type="text/javascript">
var xArray = document.getElementsByTagName("img");
for (var i=0; i<xArray.length; i++){
xArray[i].removeAttribute("width");
xArray[i].removeAttribute("height");
}
</script>
我收到的建议是将所有内容包装为一个 div,其宽度设置为我的容器宽度,然后 max-width css 属性将按比例调整我的图像大小(宽度:高度);这似乎在我给出的示例中有效,但在我的示例中不起作用,我发现图像标签中有宽度和高度属性。
所以我正在尝试删除标签。我重新运行代码,发现我查看的 HTML 是错误的。属性正在被删除。但图像的大小仍然没有达到我的预期。我在 jsfiddle.net 上看到了这个工具(单击链接查看我的示例),我可以在其中进行实验,并且它似乎工作正常。所以当我在 Firefox 中测试它时,我仍然不知道我的努力出了什么问题。最终,我希望它能够在我的 iPad 应用程序的 UIWebView 中工作。
Something is missing from this code, but I'm not sure what. I am testing it with firebug.
<script type="text/javascript">
var xArray = document.getElementsByTagName("img");
var xItem;
for (var i=0; i<xArray.length; i++){
xItem = xArray[i];
xItem.removeAttribute("width");
xItem.removeAttribute("height");
}
</script>
This is placed at the end of my html. (I intend to put this into a UIWebView with iOS, so there is no head or body element, but it appears to be working okay in the browser I am testing with.)
In my test case, there are two images, but generally, the number of images is arbitrary, so I am processing an array of elements with image tag.
I can see in the watch pane that the width and height attributes are removed from the javascript objects, but the actual elements in the html are not being updated. So I believe that xItem is probably a copy and not a pointer. What do I need to do to change the real DOM element?
UPDATE
Thanks for the quick feedback.
Here is some more info. This is an extension of a question I had in another post. The original html occasionally has images that are too large for the container, which is only 300px wide, in this case. Here is a case where the images are too large:
<style type=\"text/css\">
div{width:300px;}
p{max-width:300px;}
img {max-width:300px;}
</style>
<div style="word-wrap:break-word width:300px">
<P CLASS="Body" STYLE="text-align: center;">
<FONT SIZE="2">
<IMG TITLE="INITIATION GRAPHIC" SRC="http://www.deshow.net/d/file/travel/2009-09/arizona-grand-canyon-703-12.jpg" ALT="SUNDOOR INITIATION GRAPHIC" WIDTH="100%" HEIGHT="100%">
</FONT>
</P>
<P CLASS="Body">
<FONT SIZE="3">
<IMG TITLE="Banner" SRC="http://brown09.wikis.birmingham.k12.mi.us/file/view/Bryce_Canyon.jpg/135893343/Bryce_Canyon.jpg" ALT="Banner" WIDTH="592" HEIGHT="138">
</FONT>
</P>
</div>
<script type="text/javascript">
var xArray = document.getElementsByTagName("img");
for (var i=0; i<xArray.length; i++){
xArray[i].removeAttribute("width");
xArray[i].removeAttribute("height");
}
</script>
The advice I received was to wrap everything is a div with a width set to my container width, and then the max-width css attributes would resize my images proportionally (width:height); That appeared to work in the examples I was given, but didn't work with mine, where I found there were width and height attributes in the image tags.
So I am trying to remove the tags. I reran the code, and saw that I was looking at the HTML wrong. The attributes are being removed. But the images still are not being sized like I am expecting. I was shown this tool at jsfiddle.net (click the link to see my example), where I can experiment with this, and it seems to be working right. So I am still lost as to what is wrong with my effort when I test it in firefox. Ultimately, I want it to work inside a UIWebView in my iPad app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以直接操作它们:
You can manipulate them directly:
您的代码似乎非常适合我在这里找到: http://jsfiddle.net/jfriend00/UrHAw/。按下按钮,您将看到高度/宽度属性被删除,图像恢复到自然尺寸。
在您的代码中, xItem 是对 xArray[i] 的引用(虽然 JS 实际上没有指针,但其工作方式类似于指针),而不是副本,因此这不是您的问题。对 xItem 的任何操作都对 xArray[i] 指向的原始对象进行操作。
由于您拥有的通用 JS 代码工作正常,因此在您的具体情况下一定还有其他原因导致了您的问题。为了让我们提供帮助,您必须披露更多的环境以及您试图实现/解决的特定问题。实际 HTML 和实际代码的示例可能会有所帮助。例如,HTML 中可能存在其他约束,使图像无法更改大小,即使它们本身已经解除了人为约束。
Your code seems to work perfectly find for me here: http://jsfiddle.net/jfriend00/UrHAw/. Press the button and you will see the height/width attributes are removed and the images return to their natural size.
In your code xItem is a reference to xArray[i] (works like a pointer though JS doesn't actually have pointers), not a copy so that is not your issue. Any operation on xItem operates on the original object that xArray[i] points to.
Since the general JS code you have works fine, there must be something else in your specific case that is causing your issue. For us to help, you will have to disclose more of the environment and the particular problem you are trying to implement/solve. Samples of the actual HTML and the actual code might help. For example, there could be other constraints in the HTML that are keeping the images from changing size even though they themselves have been relieved of artificial constraint.