当某个函数针对脚本javaScript中的脚本parents元素时,如何触发脚本

发布于 2025-02-03 15:16:21 字数 1382 浏览 4 评论 0原文

当该元素成为某些函数的目标时,我需要在另一个元素的内部触发脚本。

<div id="myParent">
    <script>
       //listens for myFunction(myParent) {
            Alert("myParent was target of myFunction")
         }      
    </script>
</div>

function myFunction(target){
    //do something with the target
}

因此,预期的/期望的结果是

myFunction("myParent") //"myParent was target of myFunction"

,我知道如何添加活动听众,但是他们都没有真正做我需要的事情,或者至少我无法找到一种自定义的方法来满足我的需求。另一方面,我设法通过定位临时元素,在其中找到脚本并使用eval()触发脚本来调用脚本MyFunction()查看是否具有触发其脚本的条件。像这样:

<div id="myElement_1">
    <p>true</p>
    <script>
       //listens for myFunction(myParent) {
            "Alert("Hello")"
         }      
    </script>
</div>

<div id="myElement_2">
    <p>False</p>
    <script>
       //listens for myFunction(myParent) {
            "Alert("World!")"
         }      
    </script>
</div>


function myFunction(target){
    let checkCondition = target.getElementsByTagName("p")[0].innerText
    if(checkCondition === "true"){
        let trigger = target.getElementsByTagName("script")[0]
        eval(trigger)
    }
}

myFunction(myElement_1) // "Hello"
myFunction(myElement_2) //

我担心使用该解决方案最终会在我的功能中使用数千个检查来堵塞我的代码,因为每个潜在目标都有非常独特的触发条件。因此,聆听自己的触发器更有意义,我只是无法弄清楚如何做到这一点。

I need to trigger a script inside of another element, when that element becomes a target of certain function.

<div id="myParent">
    <script>
       //listens for myFunction(myParent) {
            Alert("myParent was target of myFunction")
         }      
    </script>
</div>

function myFunction(target){
    //do something with the target
}

So the expected/desired result is

myFunction("myParent") //"myParent was target of myFunction"

So I know how to add event listeners but none of them really do what I need them to, or at least I wasn't able to find a way to customize them to suit my needs. On the other hand I managed to call the Script by targeting the parenent element, finding the script within it and triggering it using eval(), however I am not a fan of this method as I would have to check every element that gets passed in myFunction() to see if has a condition which triggers its script. Like so:

<div id="myElement_1">
    <p>true</p>
    <script>
       //listens for myFunction(myParent) {
            "Alert("Hello")"
         }      
    </script>
</div>

<div id="myElement_2">
    <p>False</p>
    <script>
       //listens for myFunction(myParent) {
            "Alert("World!")"
         }      
    </script>
</div>


function myFunction(target){
    let checkCondition = target.getElementsByTagName("p")[0].innerText
    if(checkCondition === "true"){
        let trigger = target.getElementsByTagName("script")[0]
        eval(trigger)
    }
}

myFunction(myElement_1) // "Hello"
myFunction(myElement_2) //

Im afraid that using this solution would eventually clog up my code with thousands of checks within my functions, as each potential target has quite unique trigger conditions. So it makes more sense for the to listen for it's own triggers, I just am not able to figure out how to do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骑趴 2025-02-10 15:16:21

你不能那样做。一旦标签&lt; script&gt;出现在HTML中(XSS innionhtml),即使您删除标签,它也会被迫运行。

您可以将数据保存到HTML标签中,并通过以下方式将其拨打:

<div id="app" data-text="Hello App" />
console.log(document.querySelector("#app").getAttribute("data-text"))

或者,如果您确实需要运行脚本,则可以用 fetch

You can't do that. Once a tag <script> appears in html (except xss innerHTML), it will be forced to run even if you delete the tag.

You can save data into the html tag and call it out by:

<div id="app" data-text="Hello App" />
console.log(document.querySelector("#app").getAttribute("data-text"))

Or if you really need to run a script, you can lazyly download it with fetch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文