繁体   English   中英

jQuery.html()在Cordova制作的android应用程序中不起作用

[英]jQuery.html() not working in android application, made with cordova

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

我在这里问了一个新的问题: 无法从第二个包含的js文件中调用第一个包含的javascript文件中的函数

我原来的问题

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

首先我虽然jQuery无法正常工作...但是后来我尝试了一下:

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

而且有效。

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

UPDATE

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

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

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

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

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

更新#2

我在内部和外部尝试了html()方法

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

开始于:确保在执行“设置文本”脚本之前将<div id="content">加载到DOM,可以利用onload()来确保。 可能检查了这一点,但对其他人则要注意。

接下来,我想知道问题是否出在JQuery的.html()方法上。 文档指出“此方法使用浏览器的innerHTML属性。”

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

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

我知道在香草JS中使用innerHTML()时存在某些限制,例如<script>问题,因此,如果JQuery的.html()利用它,则可能会出现问题。

如果可以,请尝试使用香草JS通过以下方法设置#content <div>

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);
        }
    });
}

您可以在jQuery中找到方法,方法是使用Ctrl + F进行搜索,然后在查看源代码时进行搜索...例如冒号html: :。 见下图:

jQuery在源代码中搜索功能/方法

请注意底部的搜索栏。

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

  1. 警报():

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

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

从这里您可以清楚地看到,按钮触发了单击操作,该操作随后将更改“ h1”的内容,但是警报不需要对元素左右进行任何此类出价。

希望这会有所帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM