最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How cna I a add comma between words in a td if there is more than one word - Stack Overflow

programmeradmin3浏览0评论

Long backstory on this, and this seems to be the simplest solution. I have a table that will populate with user id's from a database. I need to figure out a way to add a ma between id's if there is more than one. The td id is username.

I'm not sure where to really start. I would need to index it and see if the html is > 1. And if so, add a ma between each word. Please help. This is a p.o.c forum and it doesn't suit me to add/use jQuery, so I need to do it in vanilla JavaScript.

Here is the code that pulls it from the database:

if ($this->post['post_thanks_amount'] > 0 && $this->thread['isdeleted'] == 0)
{
    $this->post['post_thanks_bit'] = fetch_thanks_bit($this->thread['forumid'], $thanks);
    $this->post['post_thanks_user'] = $post_thanks_user;
    $this->post['post_thanks_amount_formatted'] = vb_number_format($this->post['post_thanks_amount']);
    $post_thanks_box = fetch_post_thanks_template($this->post);
}

And the output with one user is this:

<tr valign="top">
    <td style="background:#E8E8E8;" colspan="2" class="alt1">
        <div>
            <a rel="nofollow" href="member.php?u=20420">ninja1</a>
            <a rel="nofollow" href="member.php?u=26154">testuser</a>
        </div>
    </td>
</tr>

so after each <a...> if there is more than one, add a ma.

Long backstory on this, and this seems to be the simplest solution. I have a table that will populate with user id's from a database. I need to figure out a way to add a ma between id's if there is more than one. The td id is username.

I'm not sure where to really start. I would need to index it and see if the html is > 1. And if so, add a ma between each word. Please help. This is a p.o.c forum and it doesn't suit me to add/use jQuery, so I need to do it in vanilla JavaScript.

Here is the code that pulls it from the database:

if ($this->post['post_thanks_amount'] > 0 && $this->thread['isdeleted'] == 0)
{
    $this->post['post_thanks_bit'] = fetch_thanks_bit($this->thread['forumid'], $thanks);
    $this->post['post_thanks_user'] = $post_thanks_user;
    $this->post['post_thanks_amount_formatted'] = vb_number_format($this->post['post_thanks_amount']);
    $post_thanks_box = fetch_post_thanks_template($this->post);
}

And the output with one user is this:

<tr valign="top">
    <td style="background:#E8E8E8;" colspan="2" class="alt1">
        <div>
            <a rel="nofollow" href="member.php?u=20420">ninja1</a>
            <a rel="nofollow" href="member.php?u=26154">testuser</a>
        </div>
    </td>
</tr>

so after each <a...> if there is more than one, add a ma.

Share Improve this question edited Jan 12, 2012 at 18:42 ChrisWue 19.1k4 gold badges61 silver badges85 bronze badges asked May 26, 2011 at 14:50 Dirty Bird DesignDirty Bird Design 5,55313 gold badges67 silver badges127 bronze badges 9
  • Can you show us your current table and what should it like after the processing ? – VirtualTroll Commented May 26, 2011 at 14:53
  • What do you mean? You want mas between the actual td contents? How will you know if you have more than one? What are you using to read from the database? – user686605 Commented May 26, 2011 at 14:54
  • Your question is quite vague. You say the id's e from the database, but what 'format' is the data returned? How are your is your datastucture for those id's in js? In an array, an object, json, ? All of these formats would have different solutions. – Stephane Gosselin Commented May 26, 2011 at 14:57
  • Apologies all, pls see edited question.thx – Dirty Bird Design Commented May 26, 2011 at 14:59
  • 2 How are the links generated on the server side? Can't you add mas in PHP? – Felix Kling Commented May 26, 2011 at 15:08
 |  Show 4 more ments

2 Answers 2

Reset to default 5

If ids is an array, then just use join:

ids = [1, 2, 3, 4];
val = ids.join(', ');
// val is now "1, 2, 3, 4"

split() automatically puts ma between words, if that's what you're looking for.

var words = 'a b c d';
document.write(words.split(' '));
发布评论

评论列表(0)

  1. 暂无评论