admin管理员组

文章数量:1313598

I'm using jQuery to add the content editable attribute to a DIV.

$('div').attr('contenteditable', 'true');

When I click it, I don't get the focus effect I get when I click on already contenteditable DIVs. It only focus when I click it another time.

This happens in every single one of my browsers – Safari 5, Chrome 18, Firefox 8. Is there anyway I can make it behave as it should?

I'm using jQuery to add the content editable attribute to a DIV.

$('div').attr('contenteditable', 'true');

When I click it, I don't get the focus effect I get when I click on already contenteditable DIVs. It only focus when I click it another time.

This happens in every single one of my browsers – Safari 5, Chrome 18, Firefox 8. Is there anyway I can make it behave as it should?

Share Improve this question asked Apr 12, 2012 at 9:41 Aen TanAen Tan 3,3456 gold badges35 silver badges53 bronze badges 4
  • 1 setup a fiddle on jsfiddle showing your problem – Baz1nga Commented Apr 12, 2012 at 9:42
  • 1 What do you mean by 'focus effect'? Neither of these appear to have it: jsfiddle/aSPKQ – Rory McCrossan Commented Apr 12, 2012 at 9:44
  • I realize I had some other click events interfering. It does work. My bad. The question is kind of invalid. What should I do with it? – Aen Tan Commented Apr 12, 2012 at 10:34
  • I am facing same issue , Unable to make editable div focus on single click -- Let me know if you have solution – Vivek Panday Commented Jun 11, 2015 at 10:24
Add a ment  | 

3 Answers 3

Reset to default 5

I realized I'm having this problem because the element is also a jquery-ui draggable and the click event has been hijacked.

http://jsfiddle/VV7BW/1/ seems to work. $('div') might be your problem, that will get all divs I think, but only apply the attr to the first one (but don't quote me on this)

Try giving your div an id, then access it with $('#id')

I had the same bug trying to add the contenteditable attribute on a mouse event. Using the mousedown event, it doesn't work. Using the click event it works fine...

The id attribute doesn't change anything in my case.

本文标签: javascriptDIV with added contenteditablequottruequot does not focus on clickStack Overflow