【问题标题】:Use a select for content editable element?对内容可编辑元素使用选择?
【发布时间】:2014-11-06 19:16:56
【问题描述】:

是否可以引入带有contenteditable 属性的选择菜单?

以下内容:

<p contenteditable="true" name="letter">a</p>

启用 contenteditable 后将成为一个选择选项:

<select name="letter">
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
</select>

【问题讨论】:

    标签: jquery contenteditable


    【解决方案1】:

    首先,您需要能够根据它是否具有contenteditable 属性来选择一个元素:

    $('[contenteditable="true"]')
    

    例如,

    HTML:

    <div>
        <p contenteditable='true'>ajdsflkjasdlfkjasdlfjdklasfD</p>
    </div>
    <div>
        <p contenteditable='true'>oqewrujfzkljvladswjoaiewnlei</p>
    </div>
    <div>
        <p contenteditable='true'>2345rtsdghwregg342534tres34</p>
    </div>
    <div>
        <p contenteditable='true'>a234trey36y34ttgfttqerertuityt</p>
    </div>
    <div>
        <p contenteditable='true'>1234rgdfs563ju6tref43f5eyrew65htew</p>
    </div>
    


    JavaScript:

    $(function() {
        $('[contenteditable="true"]').each(function() {
            var parent = $(this).parent(),
                text   = $(this).text(),
                select = function() {
                    var returnstring = '';
                    for (var i in text) {
                        returnstring += "<option value='" + text[i] + "'>" + text[i] + "</option>";
                    }
                    return "<select>" + returnstring + "</select>";
                }();
            $(this).empty();
            parent.append(select);
        });
    });
    


    这是 fiddle 的链接,您可以看到我在说什么。

    【讨论】:

      猜你喜欢
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      相关资源
      最近更新 更多