gpt4 book ai didi

javascript - 我们可以在 javascript 中使用 eval 方法创建 html 标签吗

转载 作者:行者123 更新时间:2023-11-29 22:25:21 26 4
gpt4 key购买 nike

我发现了一些在 eval 中使用 anchor 标记的代码

eval("a='http://google.co.in'+window .location.href");

代码不会在没有重定向到所需页面的同时产生错误。通过观察这段代码,我想知道是否可以使用 javascript eval() 创建 html 标签,如 eval('script tag')

最佳答案

eval 评估 JavaScript 代码。要从字符串中获取 DOM 树,可以使用以下方法之一:

  1. 设置 innerHTML DOM 元素的属性。
  2. document.write('..html here..') document.writeln('..html here...') .
    警告:当页面已经完成加载时,最后一种方法将覆盖当前文档。

例子:

  1. document.body.innerHTML = '<a href="http://stackoverflow.com/">Stack</a>';
    用单个链接替换正文。
  2. document.write('<a href="http://st.tk">Test</a>');

关于javascript - 我们可以在 javascript 中使用 eval 方法创建 html 标签吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9820096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com