How do I select all text contained in <pre> using jQuery?

怎甘沉沦 提交于 2019-12-06 23:15:31

问题


I have a contentEditable iframe with line numbers and text. The line numbers are contained in a div and all text is in a <pre>-element.

It looks like this:

<body>

  <div id="line_numbers">
    <div>1</div><div>2</div><div>3</div>
  </div>

  <pre>
    Text
  </pre>

</body>

Now, when someone presses Ctrl+A everything is selected, including the line numbers. I would like to change this behaviour to only include the contents of the <pre>.

I have set up a function that captures Ctrl+A and prevents the default operation. Now, how do I set the selection to contain everything within the <pre>?


回答1:


This answer will help you out I think; you should be able to select the pre element using jQuery and pass it into the function supplied:

SelectText($('pre')[0]);



回答2:


Have you $("#iframe_id").contents().find('pre').text()?



来源:https://stackoverflow.com/questions/2122799/how-do-i-select-all-text-contained-in-pre-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!