如何在 JavaScript 中编写 document.getElementById() 方法的简写?

javascriptjavascript 2023-08-28 04:51:31 501
摘要: document.getElementById()方法允许我们在JavaScript中使用其id来访问任何HTML元素。每个网页只能包含具有单个id的单个HTML元素。您可以使用下面的示例代码通过其id访问任何HTML元素。letelement...

如何在 JavaScript 中编写 document.getElementById() 方法的简写?

document.getElementById() 方法允许我们在 JavaScript 中使用其 id 来访问任何 HTML 元素。每个网页只能包含具有单个 id 的单个 HTML 元素。

您可以使用下面的示例代码通过其 id 访问任何 HTML 元素。

let element = document.getElementById('id'); 

在上面的代码中,我们使用了文档对象的getElementById()方法,并将id作为参数传递。

现在,如果我们需要使用 id 访问多个元素,那么使用 document.getElementById() 并不是一个好主意,但我们可以为其创建一个简写并使用它。

您可以按照以下方法为 document.getElementById() 方法创建简写。

使用箭头函数表达式编写 document.getElemenetById() 方法的简写

最简单的解决方案是使用箭头函数为 document.getElementById() 方法创建简写。我们可以创建一个箭头函数,以 id 作为参数,并在箭头函数体中使用 document.getElementById() 方法访问元素后返回该元素。

语法

您可以按照下面的语法使用箭头或匿名函数来编写 document.getElementById() 方法的简写。

let get = (id) => document.getElementById(id);
let element = get('element_id');

在上面的语法中,我们创建了 get() 箭头函数,它以 id 作为参数,使用 document.getElementById() 方法和 id 访问元素,然后返回它。

示例

在下面的示例中,我们使用匿名箭头函数为 document.getElementById() 方法创建简写。在代码中,用户可以观察到我们不需要每次使用 id 访问元素时都编写“document.getElementById()”,因为我们可以使用 get() 函数

<html>
<body>
   <h3>Using the arrow or anonymous functions to write shorthand for document.getElementById() method in JavaScript </h3>
   <div id = "output"> </div> <br>
   <div id = "test"> </div>
   <script>
      let get = (id) => document.getElementById(id);
      let output = get('output');
      output.innerHTML += "This text is written in the div with id equal to output. <br>";
      let test = get('test');
      test.innerHTML += "This text is written in the div with an id equal to the test. <br>";
   </script>
</body>
</html>

使用原型编写 document.getElementById() 方法的简写

在 JavaScript 中,大多数事物都是对象,每个对象都包含其原型。同样,原型也是一个包含创建原型链的原型的对象。我们可以向对象的原型添加方法或属性并可以使用它。在这里,我们将使用“document.getElementById”值向“document”对象添加一个属性。之后,我们可以使用该属性通过 id 访问元素。

语法

用户可以按照以下语法使用对象原型编写 document.getElementById() 方法的简写。

HTMLDocument.prototype.get = document.getElementById;
let output = document.get('output');

在上面的语法中,我们使用“HTMLDocument”对象来访问文档对象的原型。我们已将“get”属性添加到文档对象中。

示例

在下面的示例中,我们将“get”属性添加到 HTML 文档对象,并将 document.getElementById() 方法指定为值。

现在,我们可以使用“document”对象(如“document.get()”)访问 get 属性。我们可以将 id 作为“get”属性的参数传递,以通过 id 访问元素。

<html>
<body>
   <h3>Using the <i> object prototypes </i> to write shorthand for document.getElementById() method in JavaScript </h3>
   <div id = "output"> </div>
   <div id = "test"> </div>
   <script>
      HTMLDocument.prototype.get = document.getElementById;
      let output = document.get('output');
      output.innerHTML += "The output div accessed by id using the get prototype. <br>";
      let test = document.get('test');
      output.innerHTML += "The test div accessed by id using the get prototype. <br>"; 
   </script>
</body>
</html>

使用 jQuery

jQuery 是一个 JavaScript 库,允许我们编写更具可读性的 JavaScript 代码。我们可以使用 jQuery 的“$()”元素访问器通过其 id 来访问 HTML 元素。

语法

用户可以按照以下语法使用 JQuery 编写 document.getElementById() 方法的简写。

$('#id')

在上面的语法中,我们使用“#”字符来通过其 id 来访问元素。

示例

在下面的示例中,我们在 标记中添加了 JQuery CDN,以便在 HTML 中使用 JQuery。我们在 HTML 中创建了“div”元素。在 JQuery 中,我们使用“$()”访问器来访问具有 id 的元素。 “#”字符指定用户想要通过 id 访问该元素。

<html>
<head>
   <script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
</head>
<body>
   <h3>Using the <i> jQuery</i> to write shorthand for document.getElementById() method in JavaScript </h3>
   <div id = "test"> </div>
   <script>
      $('#test').html('This div is accessed via id using jQuery.');
   </script>
</body>
</html>

用户学会了使用各种方法编写 document.getElementById() 方法的简写。 JQuery 提供了一种简单而简短的代码格式来使用 id 访问元素。如果用户想使用 JavaScript,他们可以根据自己的习惯使用箭头函数或记录对象的原型。