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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码