javascript – JQuery $.html使div为空

我正在建立一个网站,主要是为了更好地编码,但我最近遇到了一个问题,我无法弄清楚如何解决.

码:

$("#user-admin-notification-area").blur(function() {
   alert(events[__event].text);
   if(this.value == '')
      $ePreview.html(events[__event].text);
   $ePreview.html(this.value);
});

上面的代码将$ePreview html设置为空字符串而不是实际预期的html,而alert提供正确的输出…我在控制台中测试了相同的代码但是没关系. (通过相同的代码,我的意思是$ePreview.html(events [__ event] .text).

事件的价值:

[
    {
        "id": "2",
        "name": "pozostały czas",
        "start": "0000-00-00 00:00:00",
        "end": "2016-01-31 23:59:59",
        "text": "<b>Uwaga!</b> Jeszcze <span data-end=\"1451606399\" data-reload=\"false\"></span> do końca składania zgłoszeń! Śpiesz się!",
        "type": "5",
        "closeable": "1"
    },
    {
        "id": "3",
        "name": "test",
        "start": "0000-00-00 00:00:00",
        "end": "0000-00-00 00:00:00",
        "text": "I should NOT be displayed! :)",
        "type": "5",
        "closeable": "1"
    },
    {
        "id": "4",
        "name": "test #2",
        "start": "2015-11-08 23:59:59",
        "end": "2015-11-30 23:59:59",
        "text": "Hi m8!",
        "type": "5",
        "closeable": "0"
    }
]

最佳答案 你的代码没有意义.如果this.value为空,则将html设置为events [__ event] .text,然后在下一行中设置空值($ePreview.html(this.value);).它应该是这样的:

$("#user-admin-notification-area").blur(function() {
   alert(events[__event].text);
   if(this.value == '')
      $ePreview.html(events[__event].text); 
   else 
      $ePreview.html(this.value);
});
点赞