对于不使用jQuery的网站,有没有一种简单的方法将jQuery包含在Chrome JavaScript控制台中?例如,在一个网站上,我想获取表中的行数。我知道jQuery很容易做到这一点。
$('element').length;
该网站不使用jQuery。我可以从命令行添加它吗?
对于不使用jQuery的网站,有没有一种简单的方法将jQuery包含在Chrome JavaScript控制台中?例如,在一个网站上,我想获取表中的行数。我知道jQuery很容易做到这一点。
$('element').length;
该网站不使用jQuery。我可以从命令行添加它吗?
当前回答
由jondavidjohn给出的最佳答案很好,但我想对其进行调整,以解决以下几点:
各种浏览器在将脚本从http加载到https页面时发出警告。如果您直接从浏览器的URL栏尝试,只需将jquery.com的协议更改为https,就会收到警告:这可能不是您要查找的站点!当我使用控制台尝试Gmail等谷歌网站时,我喜欢使用谷歌的CDN。
我唯一的问题是,我必须在控制台中包含一个版本号,我总是想要最新版本。
var jq = document.createElement('script');
jq.src = "//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
jQuery.noConflict();
其他回答
从以下位置复制所有内容:https://code.jquery.com/jquery-3.4.1.min.js
并将其粘贴到控制台中。工作完美。
现代浏览器(在Chrome、Firefox、Safari上测试)使用美元符号$实现了一些助手函数,这些函数与jQuery非常相似(如果网站没有使用window.$定义某些东西)。
这些实用程序对于选择DOM中的元素并修改它们非常有用。
文档:Chrome、Firefox
我已经在公认的解决方案基础上进行了改进
var also_unconflict = typeof $ != "undefined";
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
if(also_unconflict){
setTimeout(function(){
$=jQuery.noConflict();
console.log('jquery loaded, use jQuery instead of $')
}, 500)
}else{
console.log('jquery loaded, you can use $');
}
我在googlechrome控制台片段中使用这个函数
2020年后方法,使用:
动态导入自动执行IIFE异步的,异步的作用箭头函数
(异步()=>{等待导入('https://code.jquery.com/jquery-2.2.4.min.js')//库准备就绪console.log(jQuery)})()
如果没有异步,因为import确实返回Promise,所以我们必须使用.then():
导入('https://code.jquery.com/jquery-2.2.4.min.js').然后(()=>{console.log(jQuery)})
另一个例子
https://caniuse.com/es6-module-dynamic-import
在浏览器的JavaScript控制台中运行此命令,则jQuery应该可用。。。
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
// ... give time for script to load, then type (or see below for non wait option)
jQuery.noConflict();
注意:如果站点有与jQuery(其他库等)冲突的脚本,您仍可能遇到问题。
更新:
做得更好,创建书签会非常方便,让我们来做吧,一点反馈也很棒:
右键单击书签栏,然后单击添加页面根据需要命名它,例如Inject jQuery,并使用以下行作为URL:
javascript:(function(e,s){e.src=s;e.onload=function(){jQuery.noConflict();console.log('jQuery injected')};document.head.appendChild(e);})(document.createElement('script'),'//code.jquery.com/jquery-last.min.js')
以下是格式化代码:
javascript: (function(e, s) {
e.src = s;
e.onload = function() {
jQuery.noConflict();
console.log('jQuery injected');
};
document.head.appendChild(e);
})(document.createElement('script'), '//code.jquery.com/jquery-latest.min.js')
这里使用的是官方jQuery CDN URL,请随意使用您自己的CDN/版本。