最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select All contents of a div - Stack Overflow

programmeradmin4浏览0评论

I'm having trouble selecting all contents of the div. Here's a demo:

/

It's selecting only the first line of texts, the other lines are ignored. How can I fix this?

EDIT:

By browser is google chrome

SECOND EDIT:

Too bad i'd be answering my own question. But here i've got it to work on Google Chrome:

I've replaced

selection.setBaseAndExtent(text, 0, text, 1); 

with

selection.selectAllChildren(text);

and it worked like a charm! Please guys inform me if it works with other browsers as well.

Working Demo

I'm having trouble selecting all contents of the div. Here's a demo:

http://jsfiddle/KcX6A/304/

It's selecting only the first line of texts, the other lines are ignored. How can I fix this?

EDIT:

By browser is google chrome

SECOND EDIT:

Too bad i'd be answering my own question. But here i've got it to work on Google Chrome:

I've replaced

selection.setBaseAndExtent(text, 0, text, 1); 

with

selection.selectAllChildren(text);

and it worked like a charm! Please guys inform me if it works with other browsers as well.

Working Demo

Share Improve this question edited Jan 10, 2012 at 12:08 Jürgen Paul asked Jan 10, 2012 at 11:53 Jürgen PaulJürgen Paul 15k28 gold badges96 silver badges136 bronze badges 9
  • 1 it works good for me. Both lines are selected ! – Snake Eyes Commented Jan 10, 2012 at 11:55
  • its selecting all the contents fine..! – Sudhir Bastakoti Commented Jan 10, 2012 at 11:55
  • please include all necessary code, to understand you question, in your question as well. – Yoshi Commented Jan 10, 2012 at 11:56
  • Works for me in FF 9.0.1 and IE9, but not in Chrome 16. – Anthony Grist Commented Jan 10, 2012 at 11:56
  • Its selecting all in IE 7 but not in Chrome, please help! – Pratik Commented Jan 10, 2012 at 11:56
 |  Show 4 more ments

2 Answers 2

Reset to default 6

It seems that removing webkit specific calls to setBaseAndExtent fixes issue in latest Chrome

http://jsfiddle/KcX6A/309/

I believe you need to use innerHTML (or was it outerHTML) to get the entire text.

发布评论

评论列表(0)

  1. 暂无评论