dso89762 2015-12-21 23:39
浏览 25
已采纳

通过jQuery定位脚本选择器类

I want to target the jQuery selector class and change it some for a purpose. My code is in php string like below.

<?php echo '<span class="demo"><script>jQuery(document).ready(function($){ $(".dt").iconpicker(); });</script></span>';?>

I know there is attr selector but it may work on img or input and other tags . How about the script tag ? Is it possible ? Please suggest me ...

  • 写回答

1条回答 默认 最新

  • dpndp64206 2015-12-22 01:34
    关注

    I'm not sure why would you do this, however the answer is YES it is possible to pick the script tag using Element Selecting, with no id # nor class . prefixes, just like $('span.demo>script') or $('span.demo script') sure you can omit the span word and write it like $('.demo...).

    As shown in this PHP Fiddle - hit run or F9 to execute - I was able to pick the script tag and alter its inner html (or text) - check this script element with the dev tool inspecter - but I couldn't execute the new injected function myColor() as you can see in this image:

    enter image description here

    Code:

    <?php 
        echo '<span class="demo"><script>alert("My original span next");</script></span>';
    ?>
    <script>
        var $html = "myColor();";
        $('span.demo>script').html($html);
    
        function myColor(){
            $('span.demo').css({'background-color':'#FBB','border-color':'red'});
        }
    </script>
    

    EDIT:

    Alternatively you can give that script tag an id attribute and pick it directly, just like in this JS Fiddle again check the span.demo with the dev tool inspector. check these two links for more information:

    What is the point of using an ID attribute in a script tag?

    Giving the script tag an ID

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程