简体   繁体   English

GetElementById是否有其他选择?

[英]are there any alternatives for GetElementById?

Okay, I am new to JavaScript, so I really need help with this. 好的,我是JavaScript的新手,所以我真的需要帮助。 I have a script, which looks like this: 我有一个脚本,看起来像这样:

  var p = new Ping();

  p.ping("http://bf3.in", function(data) {
    document.getElementById("ping").innerHTML = data;
  });

 ............. some code goes here ........

<td id = 'ping'>line 1</td>
<td id = 'ping'>line 2</td>

My problem is that there are multiple tags that want to use this function on, but I can only use id="ping" on one tag, so I need an alternative for getElementById, one that would allow me to use multiple tags. 我的问题是有多个标签要使用此功能,但是我只能在一个标签上使用id =“ ping”,因此我需要getElementById的替代方法,使我可以使用多个标签。

----------------UPDATE----------------------- ---------------- UPDATE -----------------------

this is my full code http://bf3.in/ping.txt actually iam hosting some game servers so need to ping serevr ip. 这是我的完整代码http://bf3.in/ping.txt实际上是托管一些游戏服务器的,因此需要ping serevr ip。 here is where client login http://bf3.in/launcher/login.php user name : juno password : juno thanks :) 这是客户端登录http://bf3.in/launcher/login.php的位置用户名:juno密码:juno谢谢:)

An ID should only be used to refer to a single unique element. ID仅应用于引用单个唯一元素。 If you want to group elements together then use class - then you could do document.getElementsByClassName('ping'); 如果您想将元素分组在一起,则使用class然后可以执行document.getElementsByClassName('ping'); which will return an array of elements. 这将返回一个元素数组。

So: 所以:

<table>
     <td class = 'ping'>line 1</td>
     <td class = 'ping'>line 2</td>
</table>

then document.getElementsByClassName('ping'); 然后document.getElementsByClassName('ping'); will return an array containing both td elements. 将返回一个包含两个td元素的数组。 Which can be accessed using regular array indices. 可以使用常规数组索引进行访问。

But unless your table is going to display tabular data - you'd be better off using something like div instead of a table. 但是除非您的表格要显示表格数据-否则最好使用div之类的东西而不是表格。 It's not the 90's anymore. 不再是90年代了。

You could use getElementsByTagName , querySelectorAll() or you could use getElementsByClassName 您可以使用getElementsByTagNamequerySelectorAll()或可以使用getElementsByClassName

Note, all of these will return a node list. 注意,所有这些都将返回节点列表。 So if you replace getElementById with any of these will just not work. 因此,如果将getElementById替换为其中任何一个都将无法正常工作。 You have to do something like this 你必须做这样的事情

p.ping("http://bf3.in", function(data) {
   document.querySelectorAll(".ping").forEach(function(element){
      element.innerHTML = data;
})
});
<td class='ping'>line 1</td>
<td class='ping'>line 2</td>

Assuming that you are trying to achieve something like this. 假设您正在尝试实现这样的目标。

Yes, you can use the document.querySelectorAll function to query any CSS selector, or you can use document.getElementsByClassName to query based on class. 是的,您可以使用document.querySelectorAll函数查询任何CSS选择器,也可以使用document.getElementsByClassName根据类进行查询。 There's no way to query based on multiple ids, because elements can't have multiple IDs. 无法基于多个ID进行查询,因为元素不能具有多个ID。

Be careful with either one that you convert its result to an array rather than an array-like object with Array.from , so that you can use extension methods like filter and map . 请谨慎对待将结果转换为数组而不是使用Array.from转换为类似数组的对象的Array.from ,以便可以使用filtermap这样的扩展方法。

Due to the reason that an id must be unique within the document, use classes instead. 由于ID在文档中必须唯一的原因,请改用类。


Lets suppose you have the following markup: 假设您具有以下标记:

<table>
  <tr>
    <th>Ping</th>
    <th>Ping</th>
    <th>Ping</th>
  </tr>
  <tr>
    <td class="ping">{{ ping }}</td>
    <td class="ping">{{ ping }}</td>
    <td class="ping">{{ ping }}</td>
  </tr>
</table>

What you want to do is get all the elements with class .ping . 您要做的是使用.ping类获取所有元素。

Use document.querySelectorAll() which expects a string containing one or more CSS selectors separated by commas. 请使用document.querySelectorAll() ,该字符串需要一个包含一个或多个用逗号分隔的CSS选择器的字符串。 This will return a NodeList which is basically just a collection of nodes which you can iterate. 这将返回一个NodeList ,该列表基本上只是可​​以迭代的节点的集合。

 (function() { let pings = document.querySelectorAll('.ping'); function fetchPing(i) { return `ping ${i} data`; //..fetch ping data. } pings.forEach((p, i) => { p.textContent = fetchPing(i); }); })(); 
 <table> <tr> <th>Ping</th> <th>Ping</th> <th>Ping</th> </tr> <tr> <td class="ping">{{ ping }}</td> <td class="ping">{{ ping }}</td> <td class="ping">{{ ping }}</td> </tr> </table> 


Notes: 笔记:

  • If the data you are fetching is just plain text, please use textContent instead of innerHTML for performance and security reasons. 如果您要获取的数据只是纯文本,出于性能和安全原因,请使用textContent而不是innerHTML

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

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