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

$('element').length;

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


当前回答

如果您希望为用户脚本执行此操作,我会这样写:http://userscripts.org/scripts/show/123588

它将允许您包含jQuery,以及UI和任何您想要的插件。我在一个有1.5.1但没有UI的网站上使用它;这个脚本给了我1.7.1,加上UI,在Chrome或FF中没有冲突。我自己没有测试过Opera,但其他人告诉我它也适用于他们,所以这应该是一个完整的跨浏览器用户脚本解决方案,如果你需要这样做的话。

其他回答

我刚刚制作了一个带有错误处理的jQuery3.2.1书签(如果尚未加载,则仅加载,如果已加载,则检测版本,如果加载时出错,则显示错误消息)。在Chrome 27中测试。没有理由在Chrome浏览器上使用“旧”jQuery 1.9.1,因为jQuery 2.0与1.9兼容。

只需在Chrome的开发者控制台中运行以下命令或将其拖放到书签栏中:

javascript:((function(){if(typeof(jQuery)=="undefined"){window.jQuery="loading";var a=document.createElement("script");a.type="text/javascript";a.src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js";a.onload=function(){console.log("jQuery "+jQuery.fn.jquery+" loaded successfully.")};a.onerror=function(){delete jQuery;alert("Error while loading jQuery!")};document.getElementsByTagName("head")[0].appendChild(a)}else{if(typeof(jQuery)=="function"){alert("jQuery ("+jQuery.fn.jquery+") is already loaded!")}else{alert("jQuery is already loading...")}}})())

此处提供可读源代码

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

交钥匙解决方案:

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

如果您希望为用户脚本执行此操作,我会这样写:http://userscripts.org/scripts/show/123588

它将允许您包含jQuery,以及UI和任何您想要的插件。我在一个有1.5.1但没有UI的网站上使用它;这个脚本给了我1.7.1,加上UI,在Chrome或FF中没有冲突。我自己没有测试过Opera,但其他人告诉我它也适用于他们,所以这应该是一个完整的跨浏览器用户脚本解决方案,如果你需要这样做的话。

这个答案基于@genesis答案,起初我尝试了书签版本@jondavidjohn,但它不起作用,所以我将其更改为这个(将其添加到书签中):

javascript:(function(){var s = document.createElement('script');s.src = "//code.jquery.com/jquery-2.2.4.min.js";document.getElementsByTagName('head')[0].appendChild(s);console.log('jquery loaded')}());

警告语,没有在chrome中测试,但在firefox中有效,也没有在冲突环境中测试。