javascript 在控制台使用 jQuery?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15433185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using jQuery at Console?
提问by Clint Laskowski
I'm learning more about jQuery and would like to use it interactively at the JavaScript console in Chrome. Is that possible? I envision something like this, but it doesn't work:
我正在了解有关 jQuery 的更多信息,并希望在 Chrome 的 JavaScript 控制台中以交互方式使用它。那可能吗?我设想了这样的事情,但它不起作用:
> use('jquery.js')
jquery loaded
> $("span").html("Hello World!")
This would cause "Hello World!"
to be inserted between the span tags and displayed.
这将导致"Hello World!"
在 span 标签之间插入并显示。
回答by epascarello
There is no "use" so of course it will not work.
没有“使用”,所以它当然不会工作。
You can append it to a page.
您可以将其附加到页面。
var scr = document.createElement("script");
scr.src = "http://code.jquery.com/jquery-1.9.1.min.js";
document.body.appendChild(scr);
回答by Wryte
If you have jQuery included in the page that you have the console open on you should be free to use it in the console.
如果您在打开控制台的页面中包含 jQuery,您应该可以在控制台中自由使用它。
回答by Kru
回答by Clint Laskowski
Never mind. Figured it out :-) I created a simple HTML file that loaded jQuery and then went into the console. This works for me.
没关系。想通了:-) 我创建了一个简单的 HTML 文件,它加载了 jQuery,然后进入了控制台。这对我有用。
回答by Scott Puleo
You may want to check out http://jsfiddle.net/or http://codepen.io/pen/
您可能想查看http://jsfiddle.net/或http://codepen.io/pen/
You can still access the scripts you create via the chrome console and it will be easier to share with others if you have any questions.
您仍然可以通过 chrome 控制台访问您创建的脚本,如果您有任何问题,可以更轻松地与他人共享。
回答by Asif Mallik
If you want to use jQuery frequently from the console you can easily write a userscript. First, install Tampermonkey if you are on Chrome and Greasemonkey if you are on Firefox. Write a simple userscript with a use function like this:
如果您想从控制台频繁使用 jQuery,您可以轻松编写用户脚本。首先,如果您使用 Chrome,请安装 Tampermonkey,如果您使用 Firefox,请安装 Greasemonkey。使用像这样的 use 函数编写一个简单的用户脚本:
var scripts = []
function use(libname){
var src;
if(scripts.indexOf(libname)==-1){
switch(libname.toLowerCase()){
case "jquery":
src = "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js";
break;
case "angularjs":
src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js";
break;
}
}else{
console.log("Library already in use.");
return;
}
if(src){
scripts.append(libname);
var script = document.createElement("script");
script.src = src;
document.body.appendChild(scr);
}else{
console.log("Invalid Library.");
return;
}
}