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

$('element').length;

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


当前回答

使用jQueryify小册子:

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

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

其他回答

在浏览器的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/版本。

在控制台中运行

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

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

我已经在公认的解决方案基础上进行了改进

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控制台片段中使用这个函数

根据这个答案:

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

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