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 ...

图片转代码服务由CSDN问答提供 功能建议

我想定位jQuery选择器类并将其更改为某个目的。 我的代码是在php字符串中,如下所示。

 &lt;?php echo'&lt; span class =“demo”&gt;&lt; script&gt; jQuery(document).ready(function($){$(“。  dt“)。iconpicker();});&lt; / script&gt;&lt; / span&gt;';?&gt; 
   
 
 

我知道 attr 选择器,但它可以在 img input 和其他标签上工作。 脚本标签怎么样? 可能吗 ? 请建议我......

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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

    点赞 评论

相关推荐 更多相似问题