JavaScript中是否存在类似于@importCSS的内容,允许您在另一个JavaScript文件中包含JavaScript文件?
有没有一种简单的方法可以在Chrome JavaScript控制台中为不使用它的网站添加jQuery?例如,在网站上我想获取表格中的行数.我知道jQuery非常简单.
$('element').length;
Run Code Online (Sandbox Code Playgroud)
该网站不使用jQuery.我可以从命令行添加吗?
是否可以使用Javascript将CSS样式表导入html页面?如果是这样,怎么办呢?
PS javascript将托管在我的网站上,但我希望用户能够放入<head>他们网站的标签,并且它应该能够将我服务器上托管的css文件导入到当前的网页中.(css文件和javascript文件都将托管在我的服务器上).
我的本地局域网(machineA)上有一台有两台Web服务器的机器.第一个是XBMC中的内置版(在端口8080上)并显示我们的库.第二个服务器是CherryPy python脚本(端口8081),我用它来按需触发文件转换.文件转换由来自XBMC服务器提供的页面的AJAX POST请求触发.
jQuery Ajax请求
$.post('http://machineA:8081', {file_url: 'asfd'}, function(d){console.log(d)})
Run Code Online (Sandbox Code Playgroud)
请求标题 - 选项
Host: machineA:8081
User-Agent: ... Firefox/4.01
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://machineA:8080
Access-Control-Request-Method: POST
Access-Control-Request-Headers: x-requested-with
Run Code Online (Sandbox Code Playgroud)
响应标题 - 选项(状态= 200 OK)
Content-Length: 0
Access-Control-Allow-Headers: *
Access-Control-Max-Age: 1728000
Server: CherryPy/3.2.0
Date: Thu, 21 Apr 2011 22:40:29 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Content-Type: text/html;charset=ISO-8859-1
Run Code Online (Sandbox Code Playgroud)
为了排除故障,我还从http://jquery.com发出了相同的$ .post命令.这是我难倒的地方,从jquery.com开始,post请求有效,OPTIONS请求由POST发送.此交易的标题如下;
请求标题 - …
我在尝试
$(function(){
alert('Hello');
});
Run Code Online (Sandbox Code Playgroud)
它在Visual Studio中显示此错误:(TS) Cannot find name '$'..如何在TypeScript中使用jQuery?
我使用以下命令将jQuery库附加到dom:
var script = document.createElement('script');
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
Run Code Online (Sandbox Code Playgroud)
但是,当我跑:
jQuery(document).ready(function(){...
Run Code Online (Sandbox Code Playgroud)
控制台报告错误:
Uncaught ReferenceError: jQuery is not defined
Run Code Online (Sandbox Code Playgroud)
如何动态加载jQuery以及在dom中使用它?
我有这个代码:
<script type="text/javascript">
function js() {
var getJs = document.getElementById("jogo");
if (JS == true) { //if button JS is pressed - it is correct?
< script type = "text/javascript"
src = "file1.js" >
} else < script type = "text/javascript"
src = "file2.js" >
</script>
}
</script>
Run Code Online (Sandbox Code Playgroud)
它不起作用.我给了两个按钮,如果第一个按下,file1.js应该加载.如果按下第二个,file2.js应加载.
我怎样才能做到这一点?
可能重复:
在javascript文件中包含javascript文件?
你如何动态加载JavaScript文件?(想想C的#include)
有没有办法从.js文件中包含JavaScript文件?您知道:就像我如何链接.css文件顶部的.css文件一样.
xxx.js包含:
Some sort of @import yyy.js
followed by other js commands.
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
<script src="http://external/js/file/url.js">
</script>
Run Code Online (Sandbox Code Playgroud)
我想做这样的事 -
<script>
if(2>1){
//include http://external/js/file/url.js
}
</script>
Run Code Online (Sandbox Code Playgroud)
任何的想法?
我对将 JS 文件动态加载到 DOM 中需要什么感到有些困惑。
当我包含在我的 HTML 文件中时,example.js 将正常运行。
当我包含它时,它会添加到 DOM 但不会运行它。
我以前认为我必须重新创建,然后将其 append() 到标签。我感觉好像我错过了关键的一步,我只是不知道那一步是什么。
示例.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="example.js"></script><!-- working -->
<script src="add-example-dynamically.js"></script><!-- not working -->
</head>
<body>
<script>
execute( anyScriptElement ); // not working
</script>
</body>
</html>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
add-example-dynamically.js
function toExecutable( tagElement ){
// Duplicate the provided tag as a new element in order for all tags to run the 'src' attribute after adding it to the DOM
// Required to run: <script …Run Code Online (Sandbox Code Playgroud)