1

我有一个带有 的 DIV <div contentEditable="true" id="content"></div>,我的问题是在使用 javascript 发布 div 的内容后,光标仍在 DIV 内闪烁,我希望这个 div 在提交其内容后失去焦点,这就是我怎样才能得到光标提交后从DIV中删除?我已经尝试在提交后将模糊设置为此 DIV,但无济于事。

    $('.save-button').on('click',function(){
    var newBio = $("#content").text();
    newBio = $.trim(newBio);

    $.post(postdatahere,
            function(data) {
            $("#content").blur();
            $('.save-button').hide();
        });
});

.text()此外,使用从可编辑 DIV 获取数据更好还是.html()

4

2 回答 2

3

您可以尝试在提交/ajax 请求之前使 div 不可编辑,并在它的/ajax 请求结束后启用编辑。无论天气如何,都需要这样做,因为它可以解决您的问题,因为这样做是一个好习惯。

于 2013-02-15T04:05:16.800 回答
0

我想通了,您需要关注不同的用户文本输入来移动光标,因为在内容可编辑 div 模糊后光标会停留在那里。

所以你的代码看起来像这样:

    $('.save-button').on('click',function(){
    var newBio = $("#content").text();
    newBio = $.trim(newBio);

    $.post(postdatahere,
            function(data) {
            $("#SomeUserInput").focus();
            $(".save-button").focus();
            $('.save-button').hide();
        });
});

虽然不应该.save-button是一个 id 而不是一个类?如果是这样的话

    $.post(postdatahere,
            function(data) {
            $("#SomeUserInput").focus(); //blur #content by focusing on different element
            $('#save-button').focus(); // focus on the button to remove focus from #SomeUserInput and because it is going to be hidden anyways.
            $('#save-button').hide();
        });
});

(是的,这有点像黑客攻击。)

至于是否使用.text().html(),这取决于您想要什么。如果你想要样式(斜体、粗体等),你会想要使用.html(),但如果你这样做,请确保你有某种服务器端过滤器,这样你的网站上就不会出现任何错误的脚本。否则,.text()会做得很好。

希望这可以帮助!

于 2013-02-15T02:52:20.697 回答