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

$('element').length;

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


当前回答

交钥匙解决方案:

将代码放入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>”);}

其他回答

我是个叛逆者。

解决方案:不要使用jQuery。jQuery是一个抽象浏览器间DOM不一致性的库。因为您在自己的控制台中,所以不需要这种抽象。

例如:

$$('element').length

($$是控制台中document.querySelectorAll的别名。)

对于任何其他例子:我确信我能找到任何东西。特别是如果你使用的是现代浏览器(Chrome、FF、Safari、Opera)。

此外,了解DOM的工作原理不会伤害任何人,它只会提高您的jQuery水平(是的,了解更多有关javascript的知识会让您更擅长jQuery)。

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

添加到@jondavidjohn的答案中,我们还可以将其设置为书签,URL作为javascript代码。

名称:包含Jquery

Url:

javascript:var jq = document.createElement('script');jq.src = "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";document.getElementsByTagName('head')[0].appendChild(jq); setTimeout(function() {jQuery.noConflict(); console.log('jQuery loaded'); }, 1000);void(0);

然后将其添加到Chrome或Firefox的工具栏中,这样我们就不用一次又一次地粘贴脚本,只需单击书签即可。

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

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

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