问题标签 [getelementsbyclassname]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
645 浏览

firefox - firefox js getSVGDocument().getElementsByClassName('myClass') 坏了?

我有一个嵌入式 SVG,使用 js 更改 20 种路径填充颜色,它们都具有相同的类。

它适用于 Chrome,但不适用于 Firefox。我尝试了 contentDocument 和 getSVGDocument(),但没有帮助。links.length 是 20,即使在 Firefox 中也是如此,所以问题似乎出在 links[i].style.fill

有任何想法吗?

0 投票
3 回答
82458 浏览

javascript - 如何在 javascript 函数中使用 getElementsByClassName?

我不知道如何在 JavaScript 中使用多个 ID。单个 ID 和 没有问题getElementById,但是一旦我将 ID 更改为 class 并尝试使用getElementsByClassName该函数就会停止工作。我已经阅读了大约 100 篇关于该主题的帖子;仍然没有找到答案,所以我希望这里有人知道如何getElementsByClassName工作。

这是我用于测试的一些简单代码:

0 投票
1 回答
3602 浏览

javascript - 如何使用类删除元素

单击具有相同类“框”的按钮时,我想删除所有元素。我已经查找了几个人们这样做的示例,但我仍然无法弄清楚如何为我的代码执行此操作。

我有这个功能,它在单击按钮时向 DOM 添加一个框。

这是我正在尝试在单击不同按钮时清除所有新 div 的功能。

我在控制台中收到的警告是“未定义 parentElement”。我一直在试图弄清楚如何定义父元素,但我遇到了麻烦。任何帮助表示赞赏。谢谢

0 投票
3 回答
1908 浏览

javascript - 打印 CSS 类的属性列表

我尝试使用 getElementsByClassName 方法打印特定 CSS 类中的属性值列表,如下所示:

但在所有情况下,toto.length = 1。我想打印值“200px”、“50px”和“red”。有人可以帮助我吗?非常感谢您的帮助。

0 投票
4 回答
950 浏览

javascript - Perform action onclick outside of object

I'm attempting to create a javascript where when you click anywhere on the screen but on an object, it does a bit of code. (To close a dialogue box, remove nighttime mode, etc.)

I'm really unsure what I should be doing. I have thought of some sudo code, but I don't know if this can be done in javascript.

SOLUTION: (Thanks to David)

0 投票
2 回答
2225 浏览

javascript - 无法在本机 javascript 中获取类或标记元素

我试图在文档加载后隐藏一些元素。我设法做到这一点,但在使用orgetElementById时无法做到这一点。getElementsByClassNamegetElementsByTagName

我已经阅读了很多问题和答案,但找不到解决方案。这是小提琴样本。

javascript:

html:

0 投票
2 回答
5771 浏览

javascript - getElementsByClassName 不返回长度

我想获取length返回的类名

我想向一系列按钮添加事件侦听器,它们都共享相同的类名。

removeButton = document.getElementsByClassName("removeButton");

当我这样做时,removeButton.length我总是得到0,但如果我这样做:

console.log(removeButton)我可以在那里看到物品清单。

我需要长度,以便可以遍历 afor-loop并分配事件侦听器。

似乎是什么问题?

解决方案
在尝试使用 getElementsByClassName 收集元素之前检查元素是否存在

0 投票
1 回答
1431 浏览

javascript - javascript创建html for svg

i want to do this:

<object type="image/svg+xml" id="tornado5" data="bitmaps/tornado2.svg">
</object>

but i want to do it in javascript dynamically... i tried:

i want to do this:

but i want to do it in javascript dynamically... i tried:

the top html way can call the 3-line-js-color-mod at bottom and it will mod the color of the svg. But if i load the svg the js way, the svg loads, but the color mod wont work. (you can't mod the contentDocument thru an img, only thru an object i'm told).

question 11374059 asks a similar question and he was told to use img. No good here.


Check your server logs and see if there is an error related to the require_once('config.php') in Google_Client.php (If the file wasn't found, the script should have stopped).

When you do your require_once('Google_Client.php'), the following code is executed from that file. After you do your require, $apiConfig should be visible to your script.

Note that you do not touch config.php. If you need to override anything in there, you create local_config.php.

From my system with PHP 5.3 I used this script. The script as show below throws no errors. Unsetting the $apiConfig replicates your error.

0 投票
1 回答
6493 浏览

javascript - document.querySelectorAll([classname]) 在 IE9 中不起作用

可能重复:
IE9 等效于 querySelectorAll

我试图在 IE9 中运行的一些 JS 中获取所有类名为“video”的元素。我正在使用var videopanels = document.querySelectorAll(".video");哪个在 Chrome 中运行良好。

不幸的是,当我document.querySelectorAll(".video")在调试器中观看时出现此错误:

但是,当我在调试器中观看document(它显示为 a DispHTMLDocument)并打开[Methods]列表时,我看到

这是怎么回事?为什么我可以在调试器中看到它,但(显然)没有实际调用或使用它?

0 投票
1 回答
12700 浏览

javascript - 来自 document.getElementsByClassName 的元素 ID

对 JS/Ajax 来说相对较新,所以我可能在这里遗漏了一些明显的东西。假设在 javascript 中的某个时刻,我运行 ajax 来获取一些具有特定类名的 div 元素。然后我想从这些元素中的每一个中检索 html id 标记并使用该信息做一些事情(比如填充元素),就像这样。

这有可能吗?