【问题标题】:Jquery .each() not working in chromeJquery .each() 在 Chrome 中不起作用
【发布时间】:2013-10-17 09:35:33
【问题描述】:

我正在使用 Jquery Elastic。它适用于 Firefox,但在 chrome 中失败。我只有一个 textarea 我正在使用它。

HTML:

<textarea id = "tarea">Lorem Ipsum</textarea>

将textarea与插件函数绑定:

$(document).load(function() { $('#tarea').elastic() ;  }) ; 

我打开插件文件,通过alert语句找到chrome中执行停止的地方:

--- code above this initializing arrays, works fine in both FF and chrome
    return this.each( function() {
                 -- Chrome does not execute anything in the callback
                // Elastic only works on textareas
                if ( this.type !== 'textarea' ) {
                 
                    return false;
                }
       .
       .
       .
                } 
   --- return ends here , chrome does not execute anything here either. 

我用 firebug 检查了 JS 的错误,但什么也没找到。我也有一个类似的 chrome 插件,甚至没有报告任何 javascript 错误。

更新:

我将对 plug 的调用从 'load' 事件更改为 'ready' 事件。它现在有效。我不明白为什么。

【问题讨论】:

  • 你可以设置一个小提琴
  • 如果它在回调中没有得到任何东西,因为在'this'到'each'中没有任何东西,在.each()alert( this.length );行之前并检查this是否是绝对是typeof jQuery 对象,否则您可能需要重新包装它,jQuery(this)
  • this 是一个 DOM 节点(它没有属性 type),因此一旦对 'textarea' 的检查失败,函数就会返回那里?
  • @tobi 不去回调。永远不会执行检查。 @麦基警报(this.length);返回 0。typeof 返回“对象”。我也用 jquery 包装了它,但它没有帮助。
  • 问题已更新。请检查。

标签: javascript jquery google-chrome autogrow


【解决方案1】:

注意:对于 Chrome,不要期望 $ 总是 JQuery

您可以将$ 放入控制台以检查它是否返回ƒ $(selector, [startNode]) { [Command Line API] },如果是则表示未为JQuery 定义$。

这是一个好消息,您有以下方法可以尝试:

  1. 解决使用$的冲突,让jQuery没有任何歧义

首先,你可以把这段代码sn -p

var jq = document.createElement('script');
jq.src = "https://code.jquery.com/jquery-3.3.1.min.js";  /* Include any online jquery library you need */
document.getElementsByTagName('head')[0].appendChild(jq);

进入控制台, 然后将$.noConflict放入控制台,如果不返回undefined,而是返回ƒ (t){return e.$===w&amp;&amp;(e.$=Kt),t&amp;&amp;e.jQuery===w&amp;&amp;(e.jQuery=Jt),w},则说明$现在没有为JQuery定义。

接下来您可以继续输入您的区域代码,然后您会发现它现在可以正常工作了。

参考https://blog.wplauncher.com/run-jquery-in-chrome-console/


  1. 在 Chrome 中使用 .js 文件,然后调试 JavaScript 文件。

参考:Chrome DevTools Snippets

  1. 此外,对于某些特定版本的 Chrome,在 UI 中有一个选项可以设置 page context可能已删除此功能在最新版本中!)

.

参考:

https://stackoverflow.com/a/8581276/6075331, https://stackoverflow.com/a/15197993/6075331

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 2019-04-16
    • 2011-02-28
    • 2013-03-18
    • 2011-11-19
    • 2017-09-05
    • 2011-03-07
    相关资源
    最近更新 更多