使用 setAttribute 的 onClick 属性不起作用

时间:2021-05-05 11:02:04

标签: javascript python html jquery frontend

我是 JavaScript 和学习新手,我尝试仅使用 use futures::channel::mpsc; use futures::{Sink, SinkExt, Stream, StreamExt}; use std::pin::Pin; async fn foo( v: Vec<( Pin<Box<dyn Sink<u8, Error = mpsc::SendError>>>, Pin<Box<dyn Stream<Item = u8>>>, )>, ) { for (mut tx, mut rx) in v { let _ = tx.send(0); let _ = rx.next().await; } } #[tokio::main] pub async fn main() -> Result<(), Box<dyn std::error::Error>> { let (tx, rx) = mpsc::channel(32); foo(vec![(Box::pin(tx), Box::pin(rx))]).await; Ok(()) } setattributes 创建元素,但此处 createElements 不起作用,可能是什么问题。

btn.setAttribute('onclick', 'fun()')
function code() {
    let head1 = document.getElementById('head')
    let lab = document.createTextNode('Enter')
    let input1 = document.createElement('input')
    let btn = document.createElement('button');
    let lab1 = document.createTextNode('Enter Button')
    input1.setAttribute('id', 'ind')
    btn.appendChild(lab1)
    btn.setAttribute('id', 'btn11')
    btn.setAttribute.onclick = function() { fun(); };

    head1.appendChild(lab)
    head1.appendChild(input1)
    head1.appendChild(btn)
}


function fun() {
    let head = document.getElementById('hi')
    para = document.createElement('p')
    txt = document.createTextNode('hi')
    para.appendChild(txt)
    head.appendChild(para)
}

2 个答案:

答案 0 :(得分:3)

setAttribute 是一种方法,但您正在使用它,就好像它是一个属性一样。 onclick 上没有 setAttribute

您可以简单地使用:

  btn.onclick = fun();

或:如果您真的想使用 setAttribute()

btn.setAttribute('click', fun());

<script>
    function code() {
        let head1 = document.getElementById('head')
        let lab = document.createTextNode('Enter')
        let input1 = document.createElement('input')
        let btn = document.createElement('button');
        let lab1 = document.createTextNode('Enter Button')
        input1.setAttribute('id', 'ind')
        btn.appendChild(lab1)
        btn.setAttribute('id', 'btn11')
        btn.onclick = fun();

        head1.appendChild(lab)
        head1.appendChild(input1)
        head1.appendChild(btn)
    }

    
    function fun() {
        let head = document.getElementById('hi')
        para = document.createElement('p')
        txt = document.createTextNode('hi')
        para.appendChild(txt)
        head.appendChild(para)
    }


</script>
<body>
    <button onclick="code()">Enter</button>
    <div id="head"></div>
    <div id="hi"></div>
</body>

答案 1 :(得分:1)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <button onclick="code()">Enter</button>
    <div id="head"></div>
    <div id="hi"></div>

    <script>
      function code() {
        let head1 = document.getElementById("head");
        let lab = document.createTextNode("Enter");
        let input1 = document.createElement("input");
        let btn = document.createElement("button");
        let lab1 = document.createTextNode("Enter Button");
        input1.setAttribute("id", "ind");
        btn.appendChild(lab1);
        btn.setAttribute("id", "btn11");
        btn.addEventListener("click", function () {
          fun();
        });

        head1.appendChild(lab);
        head1.appendChild(input1);
        head1.appendChild(btn);
      }

      function fun() {
        let head = document.getElementById("hi");
        para = document.createElement("p");
        txt = document.createTextNode("hi");
        para.appendChild(txt);
        head.appendChild(para);
      }
    </script>
  </body>
</html>