如何通过将 jQuery 转换为 javascript 来获取ElementsByTagName?
$(document).ready(function()
{
var remote_data = [
{
'path': '/var/www/html/somefile.php',
'content': '<html><body><div class="navigation">Original data!</div></body></html>'
},
{
'path': '/var/www/html/anotherfile.php',
'content': '<html><body><div class="navigation">Original data!</div></body></html>'
}
];
var replacement = '<div class="test">New data</div>';
for (i = 0; i < remote_data.length; i++)
{
var original = $('<header>' + remote_data[i].content + '<footer>', document);
var doc = original.get();
var elems = doc.getElementsByTagName('div');
console.log(elems.length);
for (j = 0; j < elems.length; j++)
{
alert(elems[j].className);
if (elems[j].className == 'navigation')
{
alert(elems[j]);
}
}
$(original).find('.navigation').html(replacement);
document.write($(original).html());
}
});
我想调用 var elems = doc.getElementsByTagName('div'); 但这会导致错误。我无法使用 jQuery。我必须使用 javascript 从字符串中 getElementsByTagName 。这怎么能做到呢?
$(document).ready(function()
{
var remote_data = [
{
'path': '/var/www/html/somefile.php',
'content': '<html><body><div class="navigation">Original data!</div></body></html>'
},
{
'path': '/var/www/html/anotherfile.php',
'content': '<html><body><div class="navigation">Original data!</div></body></html>'
}
];
var replacement = '<div class="test">New data</div>';
for (i = 0; i < remote_data.length; i++)
{
var original = $('<header>' + remote_data[i].content + '<footer>', document);
var doc = original.get();
var elems = doc.getElementsByTagName('div');
console.log(elems.length);
for (j = 0; j < elems.length; j++)
{
alert(elems[j].className);
if (elems[j].className == 'navigation')
{
alert(elems[j]);
}
}
$(original).find('.navigation').html(replacement);
document.write($(original).html());
}
});
I want to call var elems = doc.getElementsByTagName('div');
but this causes an error. I cannot use jQuery. I must use javascript to getElementsByTagName from the string. How can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不使用
original.find('div')
?我想我不明白为什么您必须使用getElementsByTagName
。这是家庭作业吗?Why not use
original.find('div')
? I guess I don't see why you must usegetElementsByTagName
. Is this a homework assignment?