Javascript:使用 DOM 和 join()

发布于 2024-11-27 09:19:52 字数 958 浏览 0 评论 0原文

我有两个函数,一个创建一个像这样的表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘然心甜 2024-12-04 09:19:52

您遇到了一些问题:

  1. return_formatted_results 实际上并不返回任何内容(讽刺的是,因为它在名称中明确说明)
  2. join 确实仅适用于字符串。

在使 return_formatted_results 返回 div 后,尝试对 get_urls_and_display 进行此操作:

function get_urls_and_display() {
    var wait=document.getElementById("wait");
    for(var i=0; i<url.length; i++) {
        var div=return_formatted_results(i, url[i], the_title[i], the_comm[i]);
        wait.appendChild(div);
    }
}

此外,您似乎拥有三个不同的数组,它们都包含相关数据。更合适的数据结构是对象列表。您可以这样定义:

var data=[
    {
        url: "http://www.example.com/a",
        title: "Link A",
        comment: "This is the first link."
    },
    {
        url: "http://www.example.com/b",
        title: "Link B",
        comment: "This is the second link."
    }
    // ...
];

get_urls_and_display 看起来像这样:

function get_urls_and_display() {
    var wait=document.getElementById("wait");
    for(var i=0; i<data.length; i++) {
        var datum=data[i];
        var div=return_formatted_results(i, datum.url, datum.title, datum.comment);
        wait.appendChild(div);
    }
}

You've got a few problems:

  1. return_formatted_results doesn't actually return anything (ironic because it's explicitly stated in the name)
  2. join is indeed only for strings.

After you make return_formatted_results return div, try this for get_urls_and_display:

function get_urls_and_display() {
    var wait=document.getElementById("wait");
    for(var i=0; i<url.length; i++) {
        var div=return_formatted_results(i, url[i], the_title[i], the_comm[i]);
        wait.appendChild(div);
    }
}

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:

var data=[
    {
        url: "http://www.example.com/a",
        title: "Link A",
        comment: "This is the first link."
    },
    {
        url: "http://www.example.com/b",
        title: "Link B",
        comment: "This is the second link."
    }
    // ...
];

Here's what get_urls_and_display would look like with that:

function get_urls_and_display() {
    var wait=document.getElementById("wait");
    for(var i=0; i<data.length; i++) {
        var datum=data[i];
        var div=return_formatted_results(i, datum.url, datum.title, datum.comment);
        wait.appendChild(div);
    }
}
围归者 2024-12-04 09:19:52

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文