0

有什么方法可以从 div 中的文本中选择前两行。

Content in div:

/*------------------------
| some content goes here |
| inside div or any      |
| container in HTML. is  |
| it possible to select  |
| first two lines        |
| displaying in browser  |
----------------------- */

SelectedText = "some content goes here inside div or any"
4

2 回答 2

0

您好,您可以查看以下代码

 <script type="text/javascript">
function runTest(){
var str=document.getElementById("id1").value;
var arr = str.split(/[\n\r]/g);
alert(arr[0]+arr[1]);
}
</script>
<textarea id="id1">
some content goes here
inside div or any
container in HTML. is 
it possible to select
first two lines 
displaying in browser
</textarea>

<a href="#" onClick="runTest();">myLink</a>
于 2013-09-11T05:25:05.653 回答
0
var first_line = $("#divElement").text().split("\n")[1];
var second_line = $("#divElement").text().split("\n")[2];

JSFiddle 示例

于 2013-09-11T05:16:35.240 回答