【问题标题】:jQuery.html() not working in android application, made with cordovajQuery.html() 在 android 应用程序中不工作,用cordova 制作
【发布时间】:2016-12-05 10:39:47
【问题描述】:

解决方案

这不是真正的问题。问题是其中一个 javascript 没有加载。

我在这里提出了一个新问题:Can't call functions from first included javascript file from the second included js file

我原来的问题

当我在浏览器中查看我的应用程序时(在我使用 Cordova 构建/编译它之前),一切正常。但是在我用 Cordova 构建它之后,$("#content").html("test"); 在 android 4.2.2 上不再工作了。但是,它确实适用于 android 6.0.0。 alert("test");.

首先我虽然 jQuery 不起作用...但后来我尝试了这个:

$("body").click(function() {
    alert("test");
});

它成功了。

任何想法为什么 .html() 方法不起作用?

更新

id 为“content”的元素如下所示:

<div id="content">
</div>

我尝试添加一些这样的内容:

$('#content').html(`<span>test1</span>`);

在所有 android 版本上,我都使用 Google Chrome 作为我的浏览器。

更新 #2

我内外都试过html()方法

$(document).ready(function(){});

【问题讨论】:

  • 我认为这里的问题在于浏览器版本。 Android 4.2.2 使用股票浏览器,而 android 6 使用 chrome。除非你当然有人行横道。我会尝试在 setTimeout 中包含相同的代码,看看它是如何进行的。 setTimeout(function() {$('#content').html("&lt;span&gt;test1&lt;/span&gt;")}, 200)
  • 我在 4.2.2 上使用 chrome 浏览器,会将此信息添加到我的问题中。
  • @Akis 我用 200 和 1000 尝试了 setTimeout,但没有用。我在这里开始了一个新问题:stackoverflow.com/questions/41116344/…

标签: javascript android jquery cordova google-chrome


【解决方案1】:

开始: 确保在执行“设置文本”脚本之前将&lt;div id="content"&gt; 加载到DOM,可以使用onload() 来确保。可能检查过这个,但请注意其他人。

接下来,我想知道问题是否与 JQuery 的 .html() 方法有关。文档声明“此方法使用浏览器的 innerHTML 属性。”

通过以下方式单独检查innerHTML()

document.getElementById("content").innerHTML = "test";

我知道在 vanilla JS 中使用 innerHTML() 时有一定的限制,例如 &lt;script&gt; 的问题,所以如果 JQuery 的 .html() 使用它,它可能是一个问题。

如果可以,请尝试使用 vanilla JS 设置 #content &lt;div&gt; via:

document.getElementById("content").textContent = "test";

这将允许您消除 .html() 并且它使用 .innerHTML() 确实不是罪魁祸首。

编辑:这是 JQuery 的 .html() 方法,真正的问题可能在于它如何处理设置。它尝试使用innerHTML(),如果以某种方式失败,则默认为append()

见下文:

function (value) {
    return access(this, function (value) {
        var elem = this[0] || {},
            i = 0,
            l = this.length;

        if (value === undefined && elem.nodeType === 1) {
            return elem.innerHTML;
        }

        // See if we can take a shortcut and just use innerHTML
        if (typeof value === "string" && !rnoInnerhtml.test(value) && !wrapMap[(rtagName.exec(value) || ["", ""])[1].toLowerCase()]) {
            value = value.replace(rxhtmlTag, "<$1></$2>");
            try {
                for (; i < l; i++) {
                    elem = this[i] || {};
                    // Remove element nodes and prevent memory leaks
                    if (elem.nodeType === 1) {
                        jQuery.cleanData(getAll(elem, false));
                        elem.innerHTML = value;
                    }
                }
                elem = 0;
                // If using innerHTML throws an exception, use the fallback method
            } catch(e) {}
        }
        if (elem) {
            this.empty().append(value);
        }
    },
    null, value, arguments.length);
}

此外,如果html() 方法中的innerHTML() 失败,这里是调用默认值append() 的来源:

function () {
    return this.domManip(arguments, function (elem) {
        if (this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9) {
            var target = manipulationTarget(this, elem);
            target.appendChild(elem);
        }
    });
}

您可以通过使用 Ctrl + F 搜索并在查看源代码时搜索来找到 JQuery 中的方法...例如html:,使用冒号。见下图:

注意底部的搜索栏。

【讨论】:

  • .textContent 属性的唯一问题是,我无法插入我需要的 HTML 标签。可能还有其他解决方案吗?
  • 使用 JavaScript .innerHTML 属性解决了这个问题。不知道为什么它不适用于 jQuery .html() 方法。
  • 当然。如果您将 JQuery 视为一个文件,从 this download page:打开文档并按 Ctrl + F 进行查找,键入 html: 应该可以找到它。函数定义为html: function( ) {等。
  • 这里找不到函数在哪一行:code.jquery.com/jquery-3.1.1.js
  • 没关系...找到它,只是搜索“html: function( value )”。谢谢!
【解决方案2】:

我认为您的 .html() 不起作用的原因是它可能未绑定到操作/元素,而 alert() 本身是一个消息框或向用户传达信息的方式。 让我们看看使用示例:

  1. 警报():

    $("body").click(function() {
        alert("test");
    });
    
  2. html():

    $("body").click(function(){
         $("h1").html("test");
      });
    

在这里,您可以清楚地看到,按钮触发单击操作,然后会更改“h1”的内容,但警报不需要对元素进行任何此类竞标......。

希望这有帮助:)

【讨论】:

  • 那么解决办法是什么?
  • 再次检查你的 jquery 东西,因为 alert() 不需要 jquery 来工作,它是一个简单的 js 功能, 足以让 alert 工作。另一方面, html() 必然需要 jquery。
  • 但是我已经尝试在$("body").click(function() {}); 中调用alert(),所以如果这里触发了alert 方法,这意味着jquery 工作正常吗?因为$("body").click(); 是 jQuery。
  • 或者只是简单地将 html() 添加到某个元素,如示例中所示...首先尝试一些标签,然后尝试具有 id 的元素
  • 发布包含该元素的代码,内容为您尝试的 id,让我们找出问题所在
猜你喜欢
  • 1970-01-01
  • 2016-08-13
  • 1970-01-01
  • 2016-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
相关资源
最近更新 更多