【问题标题】:Javascript creating variable with html code with a new date variable [closed]Javascript使用带有新日期变量的html代码创建变量[关闭]
【发布时间】:2015-06-06 22:27:57
【问题描述】:

我不明白这里有什么问题:

var $newbox = $('<div class="ui-widget-content" id="newbox-'**+noteId**'" style="top:' + e.pageY + 'px; left: ' + e.pageX + 'px;"><span id="close">Delete comment</span><p>Your comment:</p><textarea></textarea></div>');

没有这个 +noteId 工作得很好,有了它,firebug 会给出一个缺少 ')' 的错误...顺便说一句,已经添加了这些 ****,因此您可以更容易地发现错误:)

这是工作代码:

    function makeNote(e) {
    var noteDate = new date();
    var noteId = noteDate.getTime();
    // Check the event object if the .click is on the content
    // or a created note
    if (e.eventPhase === 2) {

        // Create the new comment at the corsor postition
        var $newbox = $('<div class="ui-widget-content" id="newbox" style="top:' + e.pageY + 'px; left: ' + e.pageX + 'px;"><button id="close" class="m-btn red"><i class="icon-trash"></i> Delete</button><button id="save" class="m-btn blue"><i class="icon-plus"></i> Save</button><textarea></textarea></div>');
        $('#content').append($newbox);
        if(!$newbox.draggable()){
            $newbox.draggable();
        }

    }

}

function deleteNote() {
    $(this).parent('#newbox').remove();
}

// wait until the dom document is loaded
jQuery(document).ready(function() {

    // listen for a .click() event on the content element
    $('#content').on('click', function(e){
        makeNote(e);
    })

    // Remove the note
    $("button #close").on('click',function() {
        deleteNote();
    });

});

更新 deleteNote 函数也不起作用,这就是为什么我必须为每个笔记创建一个唯一的 id,从日期开始计算时间会给出一个随机数输出......

更新2 http://s1.postimg.org/41csvbanz/Screenshot_from_2015_06_07_13_27_15.png http://s18.postimg.org/j5bowucwp/Screenshot_from_2015_06_07_13_27_28.png

【问题讨论】:

  • 由于拼写错误,投票关闭主题;您在noteId 之后缺少+,并且您未能将Date 大写。
  • 那是一些残酷的字符串文字 HTML。尝试使用 jQuery 的 DOM element constructor 来清理您的代码。此外,选择器button #close 的格式很差 - ID 选择器应该只位于选择器的最左侧,并且选择器中应该只有一个 ID,因为没有比元素 ID 更具体的选择器.见:Specificity

标签: javascript jquery css jquery-ui


【解决方案1】:

new date(); 应为大写:new Date();

【讨论】:

    【解决方案2】:

    这里是答案,所以这个功能没有工作,但我想通了:

    $('button #close').on('click', function() {
    $this.parent('#newbox').remove();
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-28
      • 1970-01-01
      • 2022-01-05
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 2014-10-04
      相关资源
      最近更新 更多