我想創建一個div來處理一些圖像,但我不想將它附加到正文部分。用jquery動態創建div
我想使用一個js文件來保存我的代碼,這樣我就可以將它加載到我的wordpress側邊欄中。 也是我的代碼檢查是否jQuery是加載,如果它是不是會加載並運行代碼
我的代碼是:
function myJQueryCode() {
//Do stuff with jQuery
$(document).ready(function(){
$("body").append('<div id="theContentsm" class="theContentsm" style="width:100%; padding-bottom:5px;"></div>');
//I don't want append it to body just to create it so it can holds the div bellow!
$('<div class="items" id="link_'+tsource1id+'" style="width:110px; height:100px; float:left; margin:20px; background-color:#333;"></div>').html('<a href="'+tsource1+'"><img src="'+tsource1img+'" width="600" height="350" alt="xxx" style="width:104px; height:94px; float:left; margin-left:3px; margin-top:3px;" width="104" height="94"></a>').appendTo('#theContentsm');
$('<div class="brief_'+tsource1id+'" style="width:110px; height:100px; float:left; margin-top:3px; padding-bottom:3px; text-align:center;"></div>').html(tsource1caption).appendTo('#link_'+tsource1id);
});
}
//Checking if jquery is loaded..
if(typeof jQuery=='undefined') {
var headTag = document.getElementsByTagName("head")[0];
var jqTag = document.createElement('script');
jqTag.type = 'text/javascript';
jqTag.src = '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js';
jqTag.onload = myJQueryCode;
headTag.appendChild(jqTag);
} else {
myJQueryCode();
}
,我會加載它是這樣的:
<script type="text/javascript" src="http://www.mydomain.com/scripts/myjavascript.js"></script>
什麼是症狀/控制檯消息,如果有的話? – mplungjan
你能夠創建div並將其附加到正文嗎? –
@mplungjan當我使用代碼加載包含代碼的js文件時,出現..我必須使用jQuery的noConflict? –