内容可编辑光标位置
我有一个内容可编辑的 div,我希望单击编辑按钮,光标出现在 div 的开头,在它出现在末尾的那一刻。
<li style="display: list-item;" class="menu-item" contenteditable="true">
<div class="">Dior<input type="checkbox" name="selected"></div>
<ol class="">
<li style="display: list-item;" class="menu-item">
<div class=""><a href="/portfolios/charlize-theron" class="" name="">Charlize-Theron</a>
<input type="checkbox" name="selected"></div>
</li>
<li style="display: list-item;" class="menu-item">
<div class=""><a href="/portfolios/natalie-portman" class="" name="">Natalie-Portman</a>
<input type="checkbox" name="selected"></div>
</li>
<li style="display: list-item;" class="">
<div class=""><a href="/portfolios/sharon-stone-1" class="" name="">Sharon-Stone</a>
<input type="checkbox" name="selected"></div>
</li>
</ol>
<a href="#" class="edit" style="opacity: 1; ">Edit</a></li>
因此,当您立即单击“编辑”时,您必须使用箭头键返回到开头才能编辑 Dior(这是唯一真正被编辑的内容)。但我尝试仅将内容可编辑代码添加到 div 中,但它不起作用。
这是我的一些 jQuery
$(".edit").click(function(){
$(this).parent().children("div").focus(function(){
});
});
$(".edit").hover(function(){
$(this).css("opacity","0.5")
}, function(){
$(this).css("opacity","1")
});
$(".edit").parent().blur(function(){
var sortableList = $(".sortable").html();
$("#ItemDescription").val(sortableList);
});
function storeUserScribble(id) {
var scribble = $("li div").text();
localStorage.setItem('userScribble',scribble);
}
function getUserScribble() {
if ( localStorage.getItem('userScribble')) {
var scribble = localStorage.getItem('userScribble');
}
else {
}
}
function clearLocal() {
clear: localStorage.clear();
return false;
}
如果有人可以解释为什么光标没有自动位于 div 的前面,那可能会有所帮助。我想将光标位置设置为在 DIOR 之前启动 IE。
感谢您的帮助!
I have a div thats content editable and I wish to click on the edit button and the cursor appears at the start of the div, at the minute it appears at the end.
<li style="display: list-item;" class="menu-item" contenteditable="true">
<div class="">Dior<input type="checkbox" name="selected"></div>
<ol class="">
<li style="display: list-item;" class="menu-item">
<div class=""><a href="/portfolios/charlize-theron" class="" name="">Charlize-Theron</a>
<input type="checkbox" name="selected"></div>
</li>
<li style="display: list-item;" class="menu-item">
<div class=""><a href="/portfolios/natalie-portman" class="" name="">Natalie-Portman</a>
<input type="checkbox" name="selected"></div>
</li>
<li style="display: list-item;" class="">
<div class=""><a href="/portfolios/sharon-stone-1" class="" name="">Sharon-Stone</a>
<input type="checkbox" name="selected"></div>
</li>
</ol>
<a href="#" class="edit" style="opacity: 1; ">Edit</a></li>
So when you click on edit at the minute, you have to arrow key back to the start to edit the Dior (which is the only thing that really get's edited). but i've tried to add the content editable code to the div only and it doesn't work.
Here's some of my jQuery
$(".edit").click(function(){
$(this).parent().children("div").focus(function(){
});
});
$(".edit").hover(function(){
$(this).css("opacity","0.5")
}, function(){
$(this).css("opacity","1")
});
$(".edit").parent().blur(function(){
var sortableList = $(".sortable").html();
$("#ItemDescription").val(sortableList);
});
function storeUserScribble(id) {
var scribble = $("li div").text();
localStorage.setItem('userScribble',scribble);
}
function getUserScribble() {
if ( localStorage.getItem('userScribble')) {
var scribble = localStorage.getItem('userScribble');
}
else {
}
}
function clearLocal() {
clear: localStorage.clear();
return false;
}
If someone can shed some light on why the cursor isn't at the front of the div automatically that could help. I would love to set the cursor position to the start IE Before DIOR.
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jsFiddle: http://jsfiddle.net/X6Ab8/
代码:
jsFiddle: http://jsfiddle.net/X6Ab8/
Code:
检查: 在 contentEditable
另外对于 IE,另请检查 document.selection.createRange。
Check: Set cursor position on contentEditable <div>
In addition for IE's, check also document.selection.createRange.