Javascript:使用 DOM 和 join()
我有两个函数,一个创建一个像这样的表:
function return_formatted_results(result_no,addy,title,own_comment)
{
var div=document.createElement("DIV");
attr(div, 'id', 'div_id'+result_no);
var form=document.createElement("FORM");
attr(form, 'id', 'det_'+result_no);
attr(form, 'name', 'form'+result_no);
attr(form, 'method', 'post');
attr(form, 'action', '');
// ###############
// add all other objects and attributes here
// ###################
form.appendChild(table);
div.appendChild(form);
}
然后我有一个调用上述函数的函数:
function get_urls_and_display()
{
var allTables=new Array();
for(var i=0;i<url.length;i++)
{ allTables[i]=return_formatted_results(i,url[i],the_title[i],the_comm[i]); }
document.getElementById("wait").appendChild(allTables.join("\n\n"));
问题是最后一行中的 join() 抛出错误。我对使用整个 DOM 事物很陌生,就像我上面所做的那样,所以不知道该使用什么。我以前使用纯 html,然后 join() 起作用了。
谢谢!
I have two functions, one that creates a table like so:
function return_formatted_results(result_no,addy,title,own_comment)
{
var div=document.createElement("DIV");
attr(div, 'id', 'div_id'+result_no);
var form=document.createElement("FORM");
attr(form, 'id', 'det_'+result_no);
attr(form, 'name', 'form'+result_no);
attr(form, 'method', 'post');
attr(form, 'action', '');
// ###############
// add all other objects and attributes here
// ###################
form.appendChild(table);
div.appendChild(form);
}
and then I have this function that calls the above function:
function get_urls_and_display()
{
var allTables=new Array();
for(var i=0;i<url.length;i++)
{ allTables[i]=return_formatted_results(i,url[i],the_title[i],the_comm[i]); }
document.getElementById("wait").appendChild(allTables.join("\n\n"));
The problem is the join()
in the last line throws an error. I am new to working with the whole DOM thing like I did above, so have no idea what to use instead. I used to use plain html before and join() worked then.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遇到了一些问题:
return_formatted_results
实际上并不返回任何内容(讽刺的是,因为它在名称中明确说明)join
确实仅适用于字符串。在使
return_formatted_results
返回div
后,尝试对get_urls_and_display
进行此操作:此外,您似乎拥有三个不同的数组,它们都包含相关数据。更合适的数据结构是对象列表。您可以这样定义:
get_urls_and_display
看起来像这样:You've got a few problems:
return_formatted_results
doesn't actually return anything (ironic because it's explicitly stated in the name)join
is indeed only for strings.After you make
return_formatted_results
returndiv
, try this forget_urls_and_display
:Additionally, it looks like you have three different arrays all containing related data. A more suitable data structure would be a list of objects. You could define that like this:
Here's what
get_urls_and_display
would look like with that:return_formatted_results 没有返回值,即使有,它也将是一个 DOM 元素。您无法
加入
DOM 对象数组。You don't have a return value from return_formatted_results and even if you do, it would be a DOM element. You cannot
join
an array of DOM objects.