【问题标题】:Where to add addtional logic for a jQuery bookmarklet?在哪里为 jQuery 小书签添加额外的逻辑?
【发布时间】:2016-09-22 18:33:31
【问题描述】:

https://mreidsma.github.io/bookmarklets/jquerify.html

提前为这个新手 javascript 问题道歉...上面的链接非常适合加载 jQuery。但是我想添加额外的 jQuery 选择器语句,如下所示:

$('#mruSelect').hide();

但是这看起来是一个时间问题,我不确定在 jQuery 加载后我可以在以下代码中的哪个位置安全地添加它...

javascript:(function(){var el=document.createElement("div"),b=document.getElementsByTagName("body")[0],otherlib=!1,msg="";el.style.position="fixed",el.style.height="32px",el.style.width="220px",el.style.marginLeft="-110px",el.style.top="0",el.style.left="50%25",el.style.padding="5px 10px",el.style.zIndex=1001,el.style.fontSize="12px",el.style.color="#222%22,el.style.backgroundColor=%22#f99%22;function%20showMsg(){var%20txt=document.createTextNode(msg);el.appendChild(txt),b.appendChild(el),window.setTimeout(function(){txt=null,typeof%20jQuery==%22undefined%22?b.removeChild(el):(jQuery(el).fadeOut(%22slow%22,function(){jQuery(this).remove()}),otherlib&&(window.$jq=jQuery.noConflict()))},2500)}if(typeof%20jQuery!=%22undefined%22)return%20msg=%22This%20page%20already%20using%20jQuery%20v%22+jQuery.fn.jquery,showMsg();alert('test');typeof%20$==%22function%22&&(otherlib=!0);function%20getScript(url,success){var%20script=document.createElement(%22script%22);script.src=url;var%20head=document.getElementsByTagName(%22head%22)[0],done=!1;script.onload=script.onreadystatechange=function(){!done&&(!this.readyState||this.readyState==%22loaded%22||this.readyState==%22complete%22)&&(done=!0,success(),script.onload=script.onreadystatechange=null,head.removeChild(script))},head.appendChild(script)}getScript(%22//code.jquery.com/jquery.min.js%22,function(){return%20typeof%20jQuery==%22undefined%22?msg=%22Sorry,%20but%20jQuery%20was%20not%20able%20to%20load%22:(msg=%22The%20Recent%20Items%20is%20now%20hidden%22,otherlib&&(msg+=%22%20and%20noConflict().%20Use%20$jq;,%20not%20$().%22)),showMsg()})})();

我添加了一个警报语句,它可以工作,但是在那里添加 jQuery 将不起作用,因为它还不可用。

【问题讨论】:

  • 您是否尝试过以下链接。我发现它对于为纯 JS 或 JQuery 格式化书签非常有用:mrcoles.com/bookmarklet

标签: jquery dom load bookmarklet document-ready


【解决方案1】:

我确定您无法保证 jQuery 是否已加载。所以我决定在 showMsg() 之后添加以下内容;函数调用。

document.getElementById('mruSelect').style.display = 'none';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 2010-09-12
    • 2016-10-28
    • 2018-10-10
    相关资源
    最近更新 更多