简体   繁体   English

修改DOM元素的属性

[英]Modifying an attribute of a DOM element

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. 这放在我的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.) (我打算将其放入带有iOS的UIWebView中,因此没有head或body元素,但在我正在测试的浏览器中似乎可以正常工作。)

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. 我在观察窗格中看到,width和height属性已从javascript对象中删除,但是html中的实际元素并未更新。 So I believe that xItem is probably a copy and not a pointer. 因此,我相信xItem可能是副本而不是指针。 What do I need to do to change the real DOM element? 我需要怎么做才能更改真正的DOM元素?

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. 原始的html偶尔会包含对于容器来说太大的图像,在这种情况下,其宽度仅为300px。 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); 我收到的建议是将所有内容包装为一个div,并将其宽度设置为我的容器宽度,然后max-width css属性将按比例调整图像的大小(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. 在我给出的示例中,这似乎可行,但不适用于我的示例,因为我发现图像标签中存在width和height属性。

So I am trying to remove the tags. 所以我试图删除标签。 I reran the code, and saw that I was looking at the HTML wrong. 我重新运行了代码,发现我看错了HTML。 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. jsfiddle.net上向我展示了此工具(单击链接以查看我的示例),在这里可以进行试验,它似乎工作正常。 So I am still lost as to what is wrong with my effort when I test it in firefox. 因此,当我在Firefox中进行测试时,我仍然不知道自己的努力出了什么问题。 Ultimately, I want it to work inside a UIWebView in my iPad app. 最终,我希望它能在我的iPad应用程序的UIWebView中工作。

You can manipulate them directly: 您可以直接操作它们:

for (var i=0; i<xArray.length; i++){
  xArray[i].removeAttribute("width");
  xArray[i].removeAttribute("height");
}

Your code seems to work perfectly find for me here: http://jsfiddle.net/jfriend00/UrHAw/ . 您的代码似乎很适合我: 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. 在您的代码中,xItem是对xArray [i]的引用(尽管JS实际上没有指针,但它的工作方式类似于指针),而不是副本,因此这不是您的问题。 Any operation on xItem operates on the original object that xArray[i] points to. 对xItem的任何操作都对xArray [i]指向的原始对象进行操作。

Since the general JS code you have works fine, there must be something else in your specific case that is causing your issue. 由于您拥有通用的JS代码,因此可以正常工作,因此在您的特定情况下,肯定还有其他原因导致您的问题。 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. 实际HTML和实​​际代码的示例可能会有所帮助。 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. 例如,HTML中可能存在其他约束,即使图像本身已摆脱了人为约束,它们也阻止了图像的大小变化。

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

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