0

コードをテキストエリアから div にコピーしてから元に戻すと、コンテンツが同じではなくなります。

<!doctype html>
<html>
<head>
</head>
<body>
<textarea id="ta" rows="10" cols="120"><p> "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation <ul><li>sfsD</li><li>sf234fw</li></ul> ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." </p></textarea>
<br>
<button>Button</button>
<div id="container"></div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">
        $('button').click(function(){
            $('#container').html($('#ta').val());
            $('#ta').val($('#container').html());
        });
    </script>
</body>
</html>

したがって、テキストエリアから div にテキストをコピーすると、すべて問題ありません。しかし、div から textarea にコピーすると、値は次のようになります。

<p> "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation </p>
<ul><li>sfsD</li><li>sf234fw</li></ul> ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in 
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, 
sunt in culpa qui officia deserunt mollit anim id est laborum." <p></p>

なぜこれが起こるのか、それを修正する方法がわかりません! 何か案は?これは FF と chrome で発生します。

4

4 に答える 4

1

Use $('#ta').val($('#container').text());

于 2012-10-03T19:13:14.367 に答える
1

The difference that I can see is down to the fact that you are placing markup illegally, ULs are not allowed within P tags... basically both browsers in question will break the P where they find other block level elements - they are basically autocorrecting your markup for you.

If you were to copy the markup they give back to you as the markup you place in your textarea there wouldn't be a change the next time you test your button.

于 2012-10-03T19:13:59.447 に答える
1

val() returns value (which can mean many different things depending on what the element is), while html() returns markup.

you can't mix and match the two calls and expect your input to match your output - pick one or the other to meet your needs.

于 2012-10-03T19:14:22.257 に答える
0

html()マークアップを返すためです。おそらくあなたは必要text()です。

于 2012-10-03T19:10:43.620 に答える