在 Safari 中使用 getElementByClassName [英] using getElementByClassName in Safari

查看:44
本文介绍了在 Safari 中使用 getElementByClassName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试针对 Safari(移动和桌面) 并通过 Javascript 为类附加一些样式.

I'm trying to target Safari(both mobile and desktop) and append some styles for a class via Javascript.

这就是我的代码的样子.

This is how my code looks like.

(function safaristyles()
{
    if (navigator.userAgent.match(/AppleWebKit/) && ! navigator.userAgent.match(/Chrome/)) {
        var avt = document.getElementsByClassName("avatar");
        avt.style.border = "0" + "px";
        avt.style.padding = "0" + "px";
    }
})(this);

正如我在 Webkit 检查器 中看到的,这是我面临的错误.

As I see it in Webkit inspector, this is the error I'm facing.

类型问题 -'undefined' 不是一个对象(评估 'avt.style.border = "0" + "px")

我是 JS 方面的初学者,所以请耐心等待.

I'm a beginner at JS stuff, so please bear with me.

推荐答案

document.getElementsByClassName("avatar"); 将返回给你的元素是找到的元素的 HTMLCollection.

document.getElementsByClassName("avatar"); will return you elements is a HTMLCollection of found elements.

所以如果你只有一件物品,这会起作用

so if you have just one item this would work

  avt[0].style.border = "0" + "px";
    avt[0].style.padding = "0" + "px";

如果您有多个项目,请使用 for 循环.

if you have multiple items use a for loop.

这是一个类似的简单小提琴
http://jsfiddle.net/qjreK/1/

here is a simple fiddle for something similar
http://jsfiddle.net/qjreK/1/

查看这些以了解更多详情

Check these out for more details

https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByClassName

http://www.w3schools.com/htmldom/dom_using.asp

这篇关于在 Safari 中使用 getElementByClassName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆