我使用jQUery UI位置插件:http://jqueryui.com/position/將我的圖標放置在網頁上。選擇器從數據庫中抓取並使用$ myselector變量中的PHP輸出到JS。這是我當前的代碼:jQuery:未捕獲TypeError:無法讀取未定義的屬性'nodeType'
var element_selector='<?php echo $myselector;?>';
$('#inline_docxdiv .Featured.Slider').position({
my: "center",
at: "right top",
of: $(element_selector)
});
//append icons,applicable to all
$(divname<?php echo $uniqueid;?>).append('<div id="inline_docxdiv" class="<?php echo $uniqueid;?>"><div id="helpericons_display"><a class="<?php echo $title_toolsetdisplayed;?>" id="questionmarkicon_inlinedoc" title="Display Explanation"><img src="<?php echo $helper_iconpng;?>"></a><a target="_blank" href="<?php echo admin_url().'post.php?post='.$id_toolsetdisplayed.'&action=edit';?>" class="<?php echo $title_toolsetdisplayed;?>" id="sourceicon_inlinedoc" title="View source"><img src="<?php echo $helpersource_iconpng;?>"></a></div></div>');
但是圖標沒有正確附加並在控制檯中返回一個錯誤:
Uncaught TypeError: Cannot read property 'nodeType' of undefined
奇怪的是,如果我硬編碼的選擇JS代碼(不是由PHP輸出),一切正常,控制檯中沒有錯誤返回。這是我硬編碼元素選擇器的代碼:
var element_selector='.idoc-featured-slider';
有沒有辦法使用PHP來輸出選擇器而不會遇到錯誤?謝謝你的幫助。
向我們展示'<?php echo $ myselector; ?> –