为什么给动态添加的option添加事件不生效呀,其中select也是动态添加的,好困惑呀,是不是我代码写错了呀

下面就是我的代码


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
  </body> 
  <script>
    function test1() {
      // 动态创建一个select并给其添加动态的option,点击option时打印option的value。
      let arr = [
        { name: 'John' },
        { name: 'Michael' },
        { name: 'Maria' },
        { name: 'Lohgen' }
      ]
      let select = document.createElement('select')
      for (let index = 0; index < arr.length; index++) {
        let option = document.createElement('option')
        option.text = arr[index].name
        option.value = arr[index].name
        // option.onclick = ()=>{console.log(arr[index.name])}    
        select.appendChild(option)
      }
      document.body.appendChild(select)
      let options = document.getElementsByTagName('option')
      options[0].onclick = function () {
        console.log(1)    // 点击创建的option标签为什么不生效呀
      }
    }
    test1()
   
  </script>
</html>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
  </body> 
  <script>
    function test1() {
      // 动态创建一个select并给其添加动态的option,点击option时打印option的value。
      let arr = [
        { name: 'John' },
        { name: 'Michael' },
        { name: 'Maria' },
        { name: 'Lohgen' }
      ]
      let select = document.createElement('select')
      for (let index = 0; index < arr.length; index++) {
        let option = document.createElement('option')
        option.text = arr[index].name
        option.value = arr[index].name
        // option.onclick = ()=>{console.log(arr[index.name])}    
        select.appendChild(option)
      }
      document.body.appendChild(select)
      let options = document.getElementsByTagName('option')
      options[0].onclick = function () {
        console.log(1)    // 点击创建的option标签为什么不生效呀
      }
    }
    test1()
   
  </script>
</

【以下回答由 GPT 生成】

优化后的问题: 问题标题: 动态添加的option选项的点击事件无效,求解决方案。 问题描述: 我通过JavaScript动态创建了一个select元素,并添加了一些option选项。但是,我发现无法给这些动态添加的option选项添加点击事件。以下是我尝试的代码,请问我是哪里写错了呢? 是否有其他的方法可以解决这个问题呢?

代码示例:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            function test1() {
                // 动态创建一个select并给其添加动态的option,点击option时打印option的value。
                let arr = [
                    { name: 'John' },
                    { name: 'Michael' },
                    { name: 'Maria' },
                    { name: 'Lohgen' }
                ];
                let select = document.createElement('select');
                for (let index = 0; index < arr.length; index++) {
                    let option = document.createElement('option');
                    option.text = arr[index].name;
                    option.value = arr[index].name;
                    select.appendChild(option);
                }
                document.body.appendChild(select);
            }

            function addEventListeners() {
                const select = document.querySelector('select');
                select.addEventListener('change', function() {
                    const selectedOption = select.options[select.selectedIndex];
                    console.log(selectedOption.value);
                });
            }

            test1();
            addEventListeners();
        </script>
    </body>
</html>

解答: 在你的代码中,你尝试给option元素添加点击事件来打印option的value,但是由于option元素并不支持点击事件,所以你的代码无法生效。

为了达到你的需求,我提供了另一种解决方案:

  1. 使用select元素的change事件来监听选项的选择变化。
  2. 添加一个名为addEventListeners的函数,在其中使用addEventListener方法为select元素添加'change'事件监听器。
  3. 在事件监听器中,获取选中的option元素,打印其value属性的值。

这样,当你选择不同的选项时,控制台将打印所选选项的value值。

请尝试上述代码,并注意确保在选项改变时打开浏览器的控制台以查看打印的结果。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

得通过事件委托,把要添加事件的元素绑定在它的父元素上