46

如何将文本列表附加到文本区域?

<textarea id="alltext"></textarea>

<ol>
    <li onclick="addText(Hello')">Hello</li>
    <li onclick="addText(World')">World</li>
    <li onclick="addText(Earthlings')">Earthlings</li>
</ol>

<script>
    var Alltext = "";
    function addText(text) {
        Alltext += text
    }
document.getElementById("alltext").value = Alltext;
</script>

这是非常低效的,因为列表实际上很长。添加的文本正是我在 HTML 上看到的值,所以不需要输入两次,对吧?

有没有更好的方法?

4

3 回答 3

82

onclick通过将 分配给来使用事件委托<ol>。然后将event对象作为参数传递,并使用它从单击的元素中获取文本。

function addText(event) {
    var targ = event.target || event.srcElement;
    document.getElementById("alltext").value += targ.textContent || targ.innerText;
}
<textarea id="alltext"></textarea>

<ol onclick="addText(event)">
  <li>Hello</li>
  <li>World</li>
  <li>Earthlings</li>
</ol>

请注意,这种传递event对象的方法适用于较旧的 IE 以及 W3 兼容系统。

于 2013-01-21T18:35:22.457 回答
10

试试这个:

<!DOCTYPE html>
<html>
<head>
    <title>List Test</title>
    <style>
        li:hover {
            cursor: hand; cursor: pointer;
        }
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $("li").click(function(){
                $('#alltext').append($(this).text());
            });
        });
    </script>
</head>
<body>

    <h2>List items</h2>
    <ol>
        <li>Hello</li>
        <li>World</li>
        <li>Earthlings</li>
    </ol>
    <form>
        <textarea id="alltext"></textarea>
    </form>

</body>
</html>
于 2013-01-21T18:41:22.187 回答
0

托盘将带有 html 值的文本添加到 textarea 但它不起作用

价值 :

$(document).on('click', '.edit_targets_btn', function() {
            $('#add_edit_targets').modal('show');
            $('#add_edit_targets_form')[0].reset();
            $('#targets_modal_title').text('Doel bijwerken');
            $('#action').val('targets_update');
            $('#targets_submit_btn').val('Opslaan');

            $('#callcenter_targets_id').val($(this).attr("callcenter_targets_id"));
            $('#targets_title').val($(this).attr("title"));
            $("#targets_content").append($(this).attr("content"));

            tinymce.init({
                selector: '#targets_content',
                setup: function (editor) {
                    editor.on('change', function () {
                        tinymce.triggerSave();
                    });
                },
                browser_spellcheck : true,
                plugins: ['advlist autolink lists image charmap print preview anchor', 'searchreplace visualblocks code fullscreen', 'insertdatetime media table paste code help wordcount', 'autoresize'],
                toolbar: 'undo redo | formatselect | ' + ' bold italic backcolor | alignleft aligncenter ' + ' alignright alignjustify | bullist numlist outdent indent |' + ' removeformat | image | help',
                relative_urls : false,
                remove_script_host : false,
                image_list: [<?php $stmt = $db->query('SELECT * FROM images WHERE users_id = ' . $get_user_users_id); foreach ($stmt as $row) { ?>{title: '<?=$row['name']?>', value: '<?=$imgurl?>/image_uploads/<?=$row['src']?>'},<?php } ?>],
                min_height: 250,
                branding: false
            });
        });
于 2020-03-18T20:14:59.283 回答