Dynamically adding script element to a div does not execute the script

后端 未结 2 695
谎友^
谎友^ 2020-12-21 03:38

I am trying to add a script block dynamically to the document. When I do this, the script block is not getting executed.


    
2条回答
  •  忘掉有多难
    2020-12-21 04:12

    Would like to point out that the reason the 3rd snippet you provided works inside the for loop, but not outside is because you are using eval. eval takes a string and executes it as js, which is why it is working inside the loop, but using eval isn't parsing it for future use, which is causing it to be unusable elsewhere. Therefore, when you go to call it outside the loop, you get a reference error.

    UPDATE:

    If you are getting the string back with the

提交回复
热议问题