JavaScript的一些注意事项


There are a number of issues involved with getting scripts to load at the right time.
involved with  投入到,卷入到

There are a number of issues   that  involved with getting scripts to load at the right time.
很多问题都是脚本加载的时机问题。
很多问题都是涉及到脚本加载的时间。
需要在正确的时间加载脚本引起了很多问题。

involved这个词 很神奇。



例子来自:https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_is_JavaScript

document.addEventListener("DOMContentLoaded", function() {
  function createParagraph() {
    let para = document.createElement('p');
    para.textContent = 'You clicked the button!';
    document.body.appendChild(para);
  }

  const buttons = document.querySelectorAll('button');

  for(let i = 0; i < buttons.length ; i++) {
    buttons[i].addEventListener('click', createParagraph);
  }
});


错误的示例

document.addEventListener("DOMContentLoaded", function() {
  function createParagraph() {
    let para = document.createElement('p');
    para.textContent = 'You clicked the button!';
    document.body.appendChild(para);
  }

  const buttons = document.querySelectorAll('button');

  for(let i = 0; i < buttons.length ; i++) {
    buttons[i].addEventListener('click', createParagraph());
  }
});

注意前后两段代码 区别是  加粗的 createParagraph 和 createParagraph(),  后面的多了一个()


It is bad practice to pollute your HTML with JavaScript, and it is inefficient--you'd have to include the 

阅读量: 568
发布于:
修改于: