innerHTML无法运行javascript [英] innerHTML not working javascript

查看:175
本文介绍了innerHTML无法运行javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发现了一些关于这种类型的问题,但答案没有帮助。

I have already found some question of this genre but the answer didn't help.

Javascript - 没有innerHTML的div内容

Javascript:不会更改div innerHTML

我有一个名为adx-title的div,我必须更改内容。所以我做了我的ajax调用,我在调用中存储(我使用jQuery)标题我希望这个div包含:

I have a div called adx-title by id and i have to change the content. So i made my ajax call and i stored (i use jQuery) in a call the title i want this div to contain:

$('#adx-title').inneHTML = title;

Firebug报告此($('#adx-title')。inneHTML)为undefined,它我做的每次尝试都会报告更改div的内容,div被读作对象,但它没有innerHTML属性。单击一个按钮后加载脚本,因此它应该识别页面已加载的div。事实上它得到了$('#adx-title')的div。它只是不应用更改并报告innerHTML为undefined。
任何人都有类似的问题?有人可以帮忙吗?谢谢Agnese

Firebug report this ($('#adx-title').inneHTML) as undefined, and it does report so in every attempt i make to change the content of the div, which is read as an object but it doesn't have the innerHTML property. The script is loaded after i click a button so it should recognize the div as already loaded by the page. And indeed it gets the div with $('#adx-title'). it just doesn't apply the change and reports innerHTML as undefined. Anyone has had a similar issue? Anyone can help? Thanks Agnese

推荐答案

你正在使用jQuery。

You're using jQuery.

$('#adx-title').html( title );

.innerHTML 属性是DOM API。当您调用jQuery时(正如您使用 $ ),结果是一个jQuery对象,而不是DOM元素。

The .innerHTML property is part of the DOM API. When you make a call to jQuery (as you're doing with the $) the result is a jQuery object, not a DOM element.

你可以从jQuery对象中获取DOM元素,如下所示:

You can get the DOM element from the jQuery object like this:

var elem = $('#adx-title').get(0);

然而,jQuery .html() API包含对 .innerHTML 属性的访问权限,还提供了一些其他有用的簿记功能。如果您通常使用jQuery来操作DOM,那么使用 .html()是个好主意,而不是原始DOM API。

However, the jQuery .html() API wraps access to the .innerHTML property and also provides some other useful bookkeeping features. If you're using jQuery in general to manipulate the DOM, it's a good idea to use .html() and not the raw DOM API for that reason.

这篇关于innerHTML无法运行javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆