Access元素,包含当前< script> [英] Access element, which contains current <script>

查看:91
本文介绍了Access元素,包含当前< script>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下页面:

<div><script>AddSomeContent(??, 1)</script></div>
<div><script>AddSomeContent(??, 2)</script></div>

我需要更换 ?? 周围的< div> DOM对象,因此函数 AddSomeContent 可以修改它。有没有机会这样做?

I need to replace the ?? with the surrounding <div> DOM object, so the function AddSomeContent can modify it. Is there any oportunity to do this?

在做出任何其他评论之前:我没有任何其他选择。我已经尝试破解一些现有页面了,我唯一可以控制的就是< script> 的内容。

Before any other comments: I don't have any other option. I'm already trying to hack some existing page, and only thing I can control is content of the <script>.

我正在使用jquery,但我可以更改它。

I'm using jquery, but I can change it.

编辑:澄清。 AddSomeContent 看起来像:

for clarification. AddSomeContent looks like:

function AddSomeContent(somediv, parameter)
{
     $(somediv).append('there goes some data, that I dynamically create from some stuff depending on the parameter');
}

我想先用div包含参数的结果= 1 ,第二个div 参数= 2

And I want to first div contain result with parameter = 1, second div parameter=2

推荐答案

你可以试试这个,虽然它有点像黑客:

You can try this, though it's somewhat of a hack:

(function() {
  var scr = document.getElementsByTagName('script'),
      parent = scr[scr.length - 1].parentNode;
  // parent is the parent node of the last script on the page
})();

如果您的代码在< script> 这样的标签,然后当它运行时,页面上的最后一个脚本将是包含它的那个。

If you've got code in <script> tags like that, then when it runs the last script on the page will be the one that contained it.

这篇关于Access元素,包含当前&lt; script&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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