2015-12-21 82 views
0

我想定位jQuery選擇器類並將其改爲某個目的。我的代碼像下面的php字符串。jQuery的目標腳本選擇器類

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

我知道有attr選擇,但它可能在imginput和其他標籤的工作。腳本標記怎麼樣?可能嗎 ?請給我建議...

回答

1

我不知道你爲什麼會這麼做,但答案是肯定的,可以使用Element Selecting沒有ID #也不類.前綴script標籤,就像$('span.demo>script')$('span.demo script')確定您可以省略跨度單詞並將其編寫爲$('.demo...)

在本PHP Fiddle顯示 - 命中運行或F9執行 - 我能夠挑script標籤並改變其內在的HTML(或文本) - 檢查與開發工具inspecter這個腳本元素 - 但我無法執行新的注入功能myColor(),你可以在此圖像中看到:

enter image description here

代碼:

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

編輯:

或者你可以給script標籤的id屬性,並直接撿起它,就像在這個JS Fiddle再次與開發工具檢查員檢查span.demo。檢查這兩個環節的詳細資料:

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

Giving the script tag an ID