简体   繁体   English

为Javascript访问命名HTML元素的属性是什么

[英]What attribute to name HTML elements for Javascript access

What HTML attribute should I use to name HTML elements that I want to address from Javascript. 我应该使用什么HTML属性来命名我想要从Javascript中解决的HTML元素。 For example if I could use name : 例如,如果我可以使用name

<table id="some_table">
  <tr>
    <th name="date">Date</td>
    <th name="time">Time</td>
    <th name="name">Name</td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

I could select them with jQuery like this: 我可以用这样的jQuery选择它们:

$('#some_table th[name=time]')

This, however, is invalid, as name is not allowed for td . 但是,这是无效的,因为td不允许使用该name id must be unique, so I can't use it either. id必须是唯一的,所以我也不能使用它。 class could affect the styling if there is a CSS class .time . 如果有CSS类.time class可能会影响样式。 Maybe something like data-name ? 也许像data-name What is the preferred way? 什么是首选方式?

To expand on my comment, you don't "need to give the td some attribute with the column name". 要扩展我的注释,您不需要“为td赋予一些带有列名称的属性”。 The standard way would be to get the column index from the column name, then retrieve all tds with this index. 标准方法是从列名中获取列索引,然后使用此索引检索所有tds。

For example if time is the second column (index 1), all the time tds are: 例如,如果time是第二列(索引1),则所有时间tds都是:

getElementById("some_table").rows[i].cells[1]

(JavaScript has native methods to traverse tables) (JavaScript具有遍历表的本​​机方法)

You can select elements via their id value, their name, their tagName, their proximity to other elements, any of their values, or even via a selector string. 您可以通过其id值,名称,tagName,与其他元素的接近程度,任何值,甚至通过选择器字符串来选择元素。 There is no single preferred method; 没有单一的首选方法; it all depends on what you're attempting to do. 这一切都取决于你想要做什么。

In your case, you could select table rows and cells via their index if you like. 在您的情况下,如果您愿意,可以通过索引选择表行和单元格。 You could also, as you pointed out, use a data attribute - either approach would render the results you want, a way to access your elements. 正如您所指出的,您还可以使用数据属性 - 任何一种方法都可以呈现您想要的结果,这是一种访问元素的方法。

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

相关问题 在JavaScript中使用类名访问html元素 - access html elements using class name in JavaScript 最佳实践:通过 HTML id 或 name 属性访问表单元素? - Best Practice: Access form elements by HTML id or name attribute? 使用Javascript / jQuery访问ID属性不正确的HTML元素 - Using Javascript/jQuery to access HTML elements with improper id attribute 他们为什么不赞成HTML <a>元素</a>的&#39;name&#39;属性<a>?</a> - Why did they deprecate the 'name' attribute for HTML <a> elements? 通过名称属性javascript选择嵌套元素(无jquery) - Select nested elements by name attribute javascript(no jquery) Javascript 使用名称属性选择表单元素 - Javascript selecting form elements using name attribute 通过 Javascript 设置 HTML 元素的属性和样式 - Set both attribute and style for HTML elements by Javascript 如果默认值为 text/javascript,脚本元素的 HTML“nomodule”属性的目的是什么? - What’s the purpose of the HTML “nomodule” attribute for script elements if the default is text/javascript? 在javascript中按属性选择元素,其中属性名称有冒号? - Selecting elements by attribute in javascript, where attribute name has colon? 将HTML名称属性转换为Javascript对象 - Convert HTML Name Attribute to Javascript Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM