jquery追加和javascriptappendChild不适用于html标签期间
我开始尝试在 标记中添加元标记,但现在发现我无法向任何 html 标记添加任何内容。不是
、
、
,什么都没有,但它与 div id 和类一起工作得很好。
这是 html
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="append.js"></script>
<title>appendTest</title>
</head>
<body>
<div id="test">
<p>
Hello World
</p>
</div>
</body>
</html>
这是我尝试过的append.js 示例
function loadmeta(){
var meta = "<meta name='og:title' content='some random content' />";
document.getElementsByTagName('head').item(0).appendChild(meta);
}
window.onload = function(){
loadmeta();
}
/*jquery style*/
$(document).ready(function(){
$('html').append('meta name="testing" content="some content"/>');
});
$(document).ready(function(){
$('meta name="testing" content="some content"/>').appendTo('html');
});
现在我尝试了jquery 函数append 和appendTo 以及div 的p#test,我可以使用这两个函数附加到它。然而我尝试了 head、body、p,但对它们中的任何一个都不起作用。
我开始怀疑我的服务器上是否有某种安全设置正在阻止它,因为我之前已经使用append 来附加文本和css 样式并附加到正文,并且没有任何问题。
firebug 没有给出任何错误,我已经在 FF 和 Chrome 中尝试过了。
虽然我不想感觉自己像个白痴,但如果有人看到我犯的一些愚蠢的错误,我会很高兴。
谢谢
I started out trying to add meta tags in the <head>
tags but now have found that I can't add anything to any html tags. Not <body>
, <p>
, <h1>
, nothing but it works fine with div id's and classes.
Here's the html
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="append.js"></script>
<title>appendTest</title>
</head>
<body>
<div id="test">
<p>
Hello World
</p>
</div>
</body>
</html>
And here's the append.js examples I've tried
function loadmeta(){
var meta = "<meta name='og:title' content='some random content' />";
document.getElementsByTagName('head').item(0).appendChild(meta);
}
window.onload = function(){
loadmeta();
}
/*jquery style*/
$(document).ready(function(){
$('html').append('meta name="testing" content="some content"/>');
});
$(document).ready(function(){
$('meta name="testing" content="some content"/>').appendTo('html');
});
Now I tried the jquery functions both append and appendTo with the p#test for the div and I can append to it with both functions. However I tried with head, body, p and it doesn't work with any of them.
I'm beginning to wonder is there is some kind of security setting on my server that is stopping it because I have used append to append text and css styles and append to the body before and had no problem.
firebug gives no errors and I've tried it in FF and Chrome.
Although I don't want to feel like an idiot I'd love if someone sees some silly mistake I made.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码都不正确:
首先,对于您的仅 javascript 版本,appendChild 需要一个节点,而不是 HTML 字符串。
您可以执行以下操作:
您的 jQUery 方法都缺少开始 HTML 标记。
None of your codes are correct:
First of all for your javascript only version appendChild expects a node, not a string of HTML.
You could do something like:
You jQUery methods are both missing the opening HTML tag.
您没有正确创建元素
但是我什至不确定您的元标记在页面加载后是否会被解释
You are not creating the element correctly
However I am not even sure your meta tags will be interpreted after the load of the page