通过document.X访问DOM元素 [英] Accesing DOM elements through document.X

查看:97
本文介绍了通过document.X访问DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里可以找到更多关于此的信息?我以为您必须总是始终与元素进行交互 document.getElementById

Where can I see more about this? I thought that you had to do document.getElementById always for interacting with an element.

source(从Google文档中获取数据到Javascript


因此,请考虑以下JavaScript:

So consider this JavaScript:



function Data(response) {
  document.Team.tName.value = response.feed.entry[0].gs$cell.$t; // HERE !
}

以及以下HTML:

<html>
<head>
<title>
</title>
<script type="text/javascript" src="teams.js" >
</script>
</head>
<body>
<table>             
    <form name="Team">
    <tr>                
        <td>
        <input  size="19"  name="tName" readonly >
        </td>               
    </tr>
    </form>
    </table>
<script src="https://spreadsheets.google.com/feeds/cells/18WEeF3d9pJWYK1sheNHgc0KOi845cjyZgJ8x6TVisFM/1/public/values?alt=json-in-script&callback=Data&range=A1"></script>
</body>
</html>


推荐答案

使用 document.querySelector

function Data(response) {
  document.querySelector('[name=tName]').value = response.feed.entry[0].gs$cell.$t; 
}




document.querySelector



document.querySelector


返回文档中与指定选择器或选择器组匹配的第一个元素。

Returns the first Element within the document that matches the specified selector, or group of selectors.




document.querySelector('[name = tName]')


这将选择具有属性 name 和值 tName

这篇关于通过document.X访问DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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