如何让jquery根据当前文字改变文字
How to get jquery to change text based on the current text
我有一个产品搜索结果 prodArray
用于呈现我的 .ejs 脚本。它由许多基于先前查询的对象组成。每个对象将包含一个 startDate
字段,它是一个 Date()
对象。我希望 moment.js 以更令人愉悦的方式(我决定的格式)显示此日期。我使用 ejs 用我的 date.toString() 填充 <span>
标签。然后我希望 jQuery 在加载文档时选择它并以我想要的格式呈现它。
这可能不是最好的做事方式。我愿意接受任何建议。
我的.ejs代码如下:
<h1>My List of products</h1>
<% prodArray.forEach(function (product) { %>
<div class="product">
<h2><%= product.name %></h2>
<p>Starting: <span class="moment"><%= product.startDate.toString() %></span><br/>
Places: <%= product.places.max %><br/>
</p>
</div>
<% }) %>
<script>
$(document).ready(function () {
// $('.moment').text( moment( $(this).text() ).format('MMM Do YYYY hh:mm') )
// DOES NOT RUN.
// $('.moment').text( moment( $(this).contents() ).format('MMM Do YYYY hh:mm') )
// POPULATES ALL MY PRODUCTS WITH THE SAME DATE
// $('.moment').text( moment( $(this).val() ).format('MMM Do YYYY hh:mm') )
// POPULATES ALL MY PRODUCTS WITH 'INVALID DATE'
/*
$('.moment').on("ready",function () {
var datestr = $(this).text();
$(this).text(moment(datestr).format('MMM Do YYYY hh:mm'))
})
DOES NOT AFFECT ANYTHING. */
})
</script>
我尝试过的一些东西在脚本中被注释掉了,它们的效果写在每一个下面。有没有
$(.moment).nothinghappened(function () {
// blah
})
事件?
如果您有任何建议,我将不胜感激。
我想你可以做类似
<p>Starting: <span class="moment"><%= moment(product.startDate).format('dd-MMM-YYYY') %></span><br/>
所以我通过消除在客户端使用 moment 的需要解决了这个问题。如果在渲染 ejs 时将 moment 作为局部变量传递。
var moment = require('moment')
router.get('/product', function (req,res,next) {
mongoose.model('products').find({}).exec(functions (error, prodArray) {
res.render('productTemplate',{
prodArray: prodArray,
moment: moment
})
})
})
你可以像这样使用它
<p>Starting: <span class="moment"><%= moment(product.startDate).format('MMM Do YYYY hh:mm') %></span>
在您的模板中。
感谢@Sean Murin 和@Zohaib Ijaz
我有一个产品搜索结果 prodArray
用于呈现我的 .ejs 脚本。它由许多基于先前查询的对象组成。每个对象将包含一个 startDate
字段,它是一个 Date()
对象。我希望 moment.js 以更令人愉悦的方式(我决定的格式)显示此日期。我使用 ejs 用我的 date.toString() 填充 <span>
标签。然后我希望 jQuery 在加载文档时选择它并以我想要的格式呈现它。
这可能不是最好的做事方式。我愿意接受任何建议。
我的.ejs代码如下:
<h1>My List of products</h1>
<% prodArray.forEach(function (product) { %>
<div class="product">
<h2><%= product.name %></h2>
<p>Starting: <span class="moment"><%= product.startDate.toString() %></span><br/>
Places: <%= product.places.max %><br/>
</p>
</div>
<% }) %>
<script>
$(document).ready(function () {
// $('.moment').text( moment( $(this).text() ).format('MMM Do YYYY hh:mm') )
// DOES NOT RUN.
// $('.moment').text( moment( $(this).contents() ).format('MMM Do YYYY hh:mm') )
// POPULATES ALL MY PRODUCTS WITH THE SAME DATE
// $('.moment').text( moment( $(this).val() ).format('MMM Do YYYY hh:mm') )
// POPULATES ALL MY PRODUCTS WITH 'INVALID DATE'
/*
$('.moment').on("ready",function () {
var datestr = $(this).text();
$(this).text(moment(datestr).format('MMM Do YYYY hh:mm'))
})
DOES NOT AFFECT ANYTHING. */
})
</script>
我尝试过的一些东西在脚本中被注释掉了,它们的效果写在每一个下面。有没有
$(.moment).nothinghappened(function () {
// blah
})
事件?
如果您有任何建议,我将不胜感激。
我想你可以做类似
<p>Starting: <span class="moment"><%= moment(product.startDate).format('dd-MMM-YYYY') %></span><br/>
所以我通过消除在客户端使用 moment 的需要解决了这个问题。如果在渲染 ejs 时将 moment 作为局部变量传递。
var moment = require('moment')
router.get('/product', function (req,res,next) {
mongoose.model('products').find({}).exec(functions (error, prodArray) {
res.render('productTemplate',{
prodArray: prodArray,
moment: moment
})
})
})
你可以像这样使用它
<p>Starting: <span class="moment"><%= moment(product.startDate).format('MMM Do YYYY hh:mm') %></span>
在您的模板中。
感谢@Sean Murin 和@Zohaib Ijaz