0

我有以下 HTML:

<td class="mw-enhanced-rc">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;18:10&nbsp;</td>

我想使用 JavaScript 使时间变粗。

$('td[class^="mw-enhanced-rc"]').each(function() {
    this.style = this.style.fontWeight="bold";
}

↑ 不工作,我该如何解决?

那么,如何使我的页面仅在页面呈现时当前时间的一分钟内应用特殊格式?我在http://en.wikipedia.org/wiki/User:Technical_13/common.js上看到的代码:

/* Refresh my WatchList page when viewing every minute */
if (document.location.href == "http://en.wikipedia.org/wiki/Special:Watchlist") {
    setTimeout(function(){location = ''},60000);
    var timeNow = new Date();
    var minNow = timeNow.getMinutes();
    var minNow = minNow - 5;
    timeNow.setMinutes(minNow);
    var postTime = $('td.mw-enhanced-rc').html();
    for (i=0;i<6;i++) {var postTime = postTime.replace('&nbsp;', '');}
    var postTime = postTime.replace('<span class="mw-enhancedchanges-arrow-space"></span>', '');
    var postTime = postTime.replace('<abbr class="newpage" title="This edit created a new page">N</abbr>', '');
    var postTime = postTime.replace('<abbr class="minoredit" title="This is a minor edit">m</abbr>', '');
    var postTime = postTime.replace('<abbr class="botedit" title="This edit was performed by a bot">b</abbr>', '');
    var postTime = postTime.replace('<abbr class="unpatrolled" title="This edit has not yet been patrolled">!</abbr>', '');
    var postTime = postTime.split(':', '2');
    var postHour = postTime[0];
    var postMin = postTime[1];
    var timePost = new Date();
    timePost.setHours(postHour);
    timePost.setMinutes(postMin);
    if (timePost >= timeNow) {
        $('td.mw-enhanced-rc').css({
            fontWeight: 'bold',
            fontSize: '12pt'
        });
    }
}

↑ 不工作,我该如何解决?我什至尝试添加:

var timePost = timePost.getTime();
var timeNow = timeNow.getTime();

就在最后的条件 if 语句之前,它没有帮助。变得沮丧...

我还创建了一个JSfiddle来处理它。

编辑:啊哈!看起来我的条件语句正在工作,但它只评估页面上的第一个实例!然后它将 应用于$('td.mw-enhanced-rc').css({fontWeight: 'bold', fontSize: '12pt'});所有 div 元素,而不仅仅是具有符合条件的 innerHTML 的元素。谁能帮我改进一下,只更改符合条件的 div 元素的 css(可能通过注入 style= 属性)?

4

4 回答 4

4

您可以通过执行以下操作来简化它:

$('td.mw-enhanced-rc').css({fontWeight: 'bold'});
于 2013-03-12T22:17:47.233 回答
1

由于您的表达,您的代码不起作用:

this.style = this.style.fontWeight="bold";

这个表达式必须是这样的:

this.style.fontWeight = "bold";

您已经在使用 jQuery(或 Zepto),因此您可以使用 css() 方法

$('td[class^="mw-enhanced-rc"]').each(function() {
    $(this).css('fontWeight', 'bold');
}
于 2013-03-12T22:21:41.920 回答
0

尝试类似:

$('td.mw-enhanced-rc').each(function()
   {
      this.style.fontWeight = "bold";
   }
);
于 2013-03-12T22:18:44.720 回答
0

回答问题的所有部分并努力完成我正在寻找的解决方案代码是:

var timeNow = new Date();
var minNow = timeNow.getMinutes();
var minNow = minNow - 5;
timeNow.setMinutes(minNow);
var postTime = $('td.mw-enhanced-rc').html();
for (i=0;i<6;i++) {var postTime = postTime.replace('&nbsp;', '');}
var postTime = postTime.replace('<span class="mw-enhancedchanges-arrow-space"></span>', '');
var postTime = postTime.replace('<abbr class="newpage" title="This edit created a new page">N</abbr>', '');
var postTime = postTime.replace('<abbr class="minoredit" title="This is a minor edit">m</abbr>', '');
var postTime = postTime.replace('<abbr class="botedit" title="This edit was performed by a bot">b</abbr>', '');
var postTime = postTime.replace('<abbr class="unpatrolled" title="This edit has not yet been patrolled">!</abbr>', '');
var postTime = postTime.split(':', '2');
var postHour = postTime[0];
var postMin = postTime[1];
var timePost = new Date();
timePost.setHours(postHour);
timePost.setMinutes(postMin);
var timePost = timePost.getTime();
var timeNow = timeNow.getTime();
if (timePost >= timeNow) {
    var enhRC = $('td.mw-enhanced-rc').html();
    $('td[class^="mw-enhanced-rc"]').each(function() {
        this.innerHTML = this.innerHTML.replace(enhRC, '<span style="font-weight: bold; font-size: 12pt;">' + enhRC + '</span>');
    });
}
于 2013-03-13T15:43:28.610 回答