如何使span不可编辑?

时间:2016-07-28 03:50:28

标签: javascript jquery html

我在iframe中有一个span。我希望该范围不可编辑,但不起作用。我尝试了一切:指针事件,满足。什么都行不通。帮助!

  $('#tokens-menu-list').on('click', 'li', function() {
        var token       = $(this).attr('data-value');
        var token_style = "border-radius: 5px; background-color: #99CDE1;padding: 5px;cursor: context-menu;";
        $(iframeDocument).find('body').insertAtCaret("<span class='token-item' style='" + token_style + "'>" + token + "</span>");
        $('#tokens-menu').css('display', 'none');
    });



  $(iframeDocument).find('body').on('click', 'span.token-item', function() {
            $(this).css('pointer-events', 'none');
          });

2 个答案:

答案 0 :(得分:0)

添加contents()以便能够选择iframe元素:

 $(iframeDocument).contents().find('body').on('click', 'span.token-item', function(e) {
       e.preventDefault(); // This should stop the click
     // But also, make sure that
      $(this).attr("contenteditable", "false"); // This makes sure that the content can't be edited
});

答案 1 :(得分:-1)

尝试类似

的内容
$(iframeDocument).find('body').on('click', 'span.token-item', function(e) {
    e.preventDefault(); // This should stop the click
    // But also, make sure that
    $(this).attr("contenteditable", "false"); // This makes sure that the content can't be edited
});

告诉我这是否有效。

祝你好运!