使用Jquery根据其内容隐藏

我试图实现以下目标:

某个页面将包含一系列字符串,如果存在,则将其替换为数据库内容。
例如:

Title:

{{Title}}

会成为:

 

Title:

This is the title

问题是,如果从未输入{{Title}}的数据库行,则会显示{{Title}},而不是用空格替换它。 所以id喜欢做的是,使用jquery,如果.value包含{{,隐藏整个元素,以同样的方式显示:none会。

这可能吗?

提前致谢。

 $(function () // when DOM is ready for manipulation, do: { // for each of the p-elements in the DOM (add your own context or class-scope // or whatever you can do to narrow down the list of elements here): $("p").each(function () { // cache the element (always a good idea when doing multiple operations // on the same entity): var that = $(this); // if the text of the element is "{{Title}}" then hide the element: if (that.text() == "{{Title}}") that.hide(); // alternatively you can do: // if the the characters "{{" exists at any position in the text of the // element, hide it: if (that.text().indexOf("{{") > -1) that.hide(); }); }); 
 $("p.value:contains('{{')").hide(); 

编辑:
声称此代码较慢。 应该说这是相当基本的,实际上运行速度快了3倍
检查此示例(第一个更慢): http : //jsbin.com/ikite

尝试:

 $('p.value').each(function(i,e){ if ($(e).text().match(/^{{.*}}$/)) { $(e).hide(); } }); 

你能不能只给出pa类的头衔?

 

Title:

然后隐藏/显示它:

 if(title != '') { $('p.title.value').text(title); } else { $('.title').hide(); }