te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - HTML generation in JS code - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML generation in JS code - Stack Overflow

programmeradmin3浏览0评论

I'd like to know your thoughts about HTML code generation in my JS code.

I just think that the html.push("<tag>" + something + "</tag>") style pretty annoying. I've already tried something with templates inside my HTML file (and put some placeholders therein), and then used its content to a replace the placeholders to my real values.

But maybe you guys have other ideas, possibly one using jQuery.

I'd like to know your thoughts about HTML code generation in my JS code.

I just think that the html.push("<tag>" + something + "</tag>") style pretty annoying. I've already tried something with templates inside my HTML file (and put some placeholders therein), and then used its content to a replace the placeholders to my real values.

But maybe you guys have other ideas, possibly one using jQuery.

Share Improve this question edited Jun 4, 2009 at 19:49 sth 230k56 gold badges287 silver badges369 bronze badges asked Jun 4, 2009 at 19:47 gsbgsb 1,2291 gold badge16 silver badges31 bronze badges 2
  • What did you not like about a templating solution? – Nosredna Commented Jun 4, 2009 at 19:51
  • could you give more details and/or an example? I'm not sure what you're trying to do. Are you trying to construct HTML to call something.innerHTML = myhtmlstring, or are you trying to use the DOM appendChild / replaceChild functions? – Jason S Commented Jun 4, 2009 at 19:52
Add a ment  | 

6 Answers 6

Reset to default 5

jQuery is the way to go. You can do things like this:

// create some HTML
var mySpan = $("<span/>").append("Something").addClass("highlight");
  • it is cross-browser patible,
  • it is an easy to use syntax

There is also a templating plugin.

You can use createelement, appendchild, and innerHTML to do this.

Here's an article from A List Apart that uses these functions in the process of generating a dropdown menu.

jQuery has javascript template plugins like jBind and jTemplate. I haven't used them myself but I do remend jQuery whenever possible.

A note on html generation, it is not searchable by search engines in most cases.

I'm a big fan of how PrototypeJS handles templates.

First you create an instance of the Template class and pass it a string containing the template HTML.

var tpl = new Template('Here is a link to <a href="#{link}">#{sitename}</a>');

Then you pass it data containing the values to replace within the template.

$('someDiv').innerHTML = tpl.evaluate( {link: 'http://www.stackoverflow.', sitename: 'StackOverflow'} );

In the above example, I have a div with id="someDiv" and I'm replacing the contents of the div with the result of the template evaluation.

Resig has a little blog entry on creating a very lightweight template system.

There are a bunch of JQuery function that support this. In particular, you should look at append(content), appendTo(content) prepend(content) prependTo(content), replaceWith(content), after(content), before(content), insertAfter(content), and insertBefore(content).

发布评论

评论列表(0)

  1. 暂无评论