如何执行< script>代码在一个JavaScript附加 [英] How to execute <script> code in a javascript append

查看:77
本文介绍了如何执行< script>代码在一个JavaScript附加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用只有javascript的插件。我需要动态地创建一个带广告的DIV元素。

I'm working with a plugin that is only javascript. I need to have it dynamically create a DIV element with an advertisement in it.

我无法弄清楚为什么这不起作用:

I can't figure out why this doesn't work:

$(this).append('<div class="overlay-background">Advertisement

     <script type="text-javascript">

          GA_googleFillSlot("blog_landing_right_rectangle_300x250");

     </script>'

它导致使用Hello World创建的元素,但它不会执行GA-googleFillSlot函数。

It results in the element created with "Hello World" but it does not execute the GA-googleFillSlot function.

推荐答案

将HTML附加到DOM不会导致浏览器评估所有附加HTML中的任何脚本标记。

appending HTML into the DOM does not cause the browser to evaluate any script tags in said appended HTML.

你真的想要,你可以通过使用 eval()来评估javascript:

If you really wanted to, you could evaluate the javascript by using eval():

eval($(this).find("script").text());

这篇关于如何执行&lt; script&gt;代码在一个JavaScript附加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆