如何从书签中加载外部css,js并将div附加到DOM?

时间:2009-12-21 09:32:14

标签: javascript jquery bookmarklet

我正在尝试构建一个执行以下操作的书签:

1.)加载位于我服务器上的外部js(完成)

javascript:function%20loadScript(scriptURL)%20{%20var%20scriptElem%20=%20document.createElement('SCRIPT');%20scriptElem.setAttribute('language',%20'JavaScript');%20scriptElem.setAttribute('src',%20scriptURL);%20document.body.appendChild(scriptElem);}%20loadScript('http://127.0.0.1/main.js?x='+Math.random());

2.)这个外部javascript依次加载jquery和jquery ui,而不是在当前站点DOM中添加div。 (不能让这个工作)

function loadScripts(scriptURL) {


 var scriptElem = document.createElement('SCRIPT');
 scriptElem.setAttribute('language', 'JavaScript');
 scriptElem.setAttribute('src', scriptURL);
 void(document.body.appendChild(scriptElem));
}


loadScripts('http://127.0.0.1/js/jquery-1.3.2.min.js');
loadScripts('http://127.0.0.1/js/jquery-ui-1.7.2.custom.min.js');


var head = document.getElementsByTagName('head')[0]; 
$(document.createElement('link')).attr({type: 'text/css', href: 'http://127.0.0.1/css/redmond/jquery-ui-1.7.2.custom.css', rel: 'stylesheet'}).appendTo(head);



$(document).ready(function(){
    div = $("<div>").html("Loading......");
    $("body").prepend(div);
});

我遇到的问题是我无法附加具有特定id的div,想法是使用jqueryui的dialog()函数,以便在我使用的任何网站上显示可调整大小的可移动对话框书签上。

$("#dialog").dialog(); 

你可以想象我需要将我的内容加载到特定的div中,这样我就不会搞乱加载bookmarklet的网站设计。

3.)刚刚打开的对话框具有远程php文件的内容,该文件基本上解析了页面。

脚本完成我只需要在我正在尝试构建的对话框中加载它。

请帮助我,因为我真的坚持这个!感谢。

1 个答案:

答案 0 :(得分:1)

这有帮助吗?这是给你带来问题的部分吗?

$("<div id='id'></div>").html("Loading......");

jQuery文档说:

  

所有HTML必须格式正确,否则可能无法在所有浏览器中正常运行。这包括$("<span>")可能不起作用但$("<span/>")将起作用的情况(请注意XML样式的结束斜杠)。