对于不使用jQuery的网站,有没有一种简单的方法将jQuery包含在Chrome JavaScript控制台中?例如,在一个网站上,我想获取表中的行数。我知道jQuery很容易做到这一点。

$('element').length;

该网站不使用jQuery。我可以从命令行添加它吗?


当前回答

根据这个答案:

fetch('https://code.jquery.com/jquery-latest.min.js').then(r => r.text()).then(r => eval(r))

出于某种原因,我必须执行两次以获得新的“$”(我也必须使用其他方法),但它有效。

如果你的浏览器不是那么现代,这是相当的:

fetch('http://code.jquery.com/jquery-latest.min.js').then(function(r){return r.text()}).then(function(r){eval(r)})

其他回答

使用jQueryify小册子:

https://web.archive.org/web/20190502132317/http://marklets.com/jQuerify.aspx

这将使它成为一个可点击的书签,而不是复制粘贴其他答案中的代码。

在控制台中运行

var script = document.createElement('script');script.src = "https://code.jquery.com/jquery-3.4.1.min.js";document.getElementsByTagName('head')[0].appendChild(script);

它创建一个新的脚本标记,用jQuery填充并附加到头部。

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

交钥匙解决方案:

将代码放入code_here函数中。并防止没有HEAD标记的HTML。

(功能(头部){var jq=document.createElement('script');jq.src=“https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js";((head&&head[0])||document.firstChild).appendChild(jq);})(document.getElementsByTagName('head'));函数jQueryReady(){if(window.jQuery){jQuery.noConflict();您的代码_此处(jQuery);}其他{setTimeout(jQueryReady,100);}}jQueryReady();函数yourCode_here($){console.log(“OK”);$(“body”).html(“<h1>你好!</h1>”);}

直观的单线图

document.write(unescape('%3Cscript src="https://code.jquery.com/jquery-3.1.1.min.js"%3E%3C/script%3E’))

您可以更改src地址。我引用了ReferenceError:找不到变量:jQuery