使用为内容选择编辑元素?(Use a select for content editable ele

2019-10-20 22:39发布

是否有可能把在选择菜单与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>

Answer 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);
    });
});


这里是一个链接到小提琴 ,所以你可以看到我在谈论。



文章来源: Use a select for content editable element?