简体   繁体   中英

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

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> ?

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]);

你有$("#iframe_id").contents().find('pre').text()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM