简体   繁体   中英

HTML making Div editable by clicking on the edit button

I am trying to make both DIV's editable when I click on the edit icon on bottom left. How can I do that?

<li class="question" id="question2">

    <div class="question-header curves dense-shadows">

        What color is the sky? 
    </div>
    <div class="question-content dense-shadows">
        <ol type="A">
            <li><input type="radio" id="q2a1" name="question2" /> Red</li>
            <li><input type="radio" id="q2a2" name="question2" /> Green</li>
            <li><input type="radio" id="q2a3" name="question2" /> Blue</li>
            <li><input type="radio" id="q2a4" name="question2" /> Brown</li>
        </ol>
        <div style="text-align:right">
            <a href="#"><span style="margin-left:5px;"><img src="images/editbutton.png" 
                onmouseover="this.src='images/editbuttonhover.png'" onmouseout="this.src='images/editbutton.png'" title="Edit" alt="edit" /></span></a>
            <a href="#"><span style="margin-left:5px;"><img src="images/deletebutton.png" 
                onmouseover="this.src='images/deletebuttonhover.png'" onmouseout="this.src='images/deletebutton.png'" title="Delete" alt="delete"/></span></a>
        </div>
    </div>
</li>

Well, you can do something like this:

<li class="question" id="question2">
    <div class="question-header curves dense-shadows">
        What color is the sky? 
    </div>
    <div class="question-content dense-shadows">
        <ol type="A">
            <li><input type="radio" id="q2a1" name="question2" /> Red</li>
            <li><input type="radio" id="q2a2" name="question2" /> Green</li>
            <li><input type="radio" id="q2a3" name="question2" /> Blue</li>
            <li><input type="radio" id="q2a4" name="question2" /> Brown</li>
        </ol>
        <div style="text-align:right">
            <a href="#" class="edit"><span style="margin-left:5px;"><img src="images/editbutton.png" 
                onmouseover="this.src='images/editbuttonhover.png'" onmouseout="this.src='images/editbutton.png'" title="Edit" alt="edit" /></span></a>
            <a href="#" class="delete"><span style="margin-left:5px;"><img src="images/deletebutton.png" 
                onmouseover="this.src='images/deletebuttonhover.png'" onmouseout="this.src='images/deletebutton.png'" title="Delete" alt="delete"/></span></a>
        </div>
    </div>
</li>

<script>    
$('a.edit').on('click',function(){
    $(this).closest('li').find('.question-header, .question-content').attr('contenteditable','true');
});
</script>

Note that I have added class="edit" and class="delete" to your buttons.

Well you question is not clear but i will try to guess. If you want to make div editable you can mark it with contenteditable

so to do that replace your code

 <a href="#"><span style="margin-left:5px;"><img src="images/editbutton.png" 
                onmouseover="this.src='images/editbuttonhover.png'" onmouseout="this.src='images/editbutton.png'" title="Edit" alt="edit" /></span></a>

with

<a href="javascript:void(0)" class="edit"><span style="margin-left:5px;"><img src="images/editbutton.png" 
                    onmouseover="this.src='images/editbuttonhover.png'" onmouseout="this.src='images/editbutton.png'" title="Edit" alt="edit" /></span></a>

then add script

<script>
    $("a.edit").click(function(){
       $("div.question-content").attr("contenteditable", "true");
    });
    </script>

Please note that i have added class to link and replaces HREF

Here is my example http://jsfiddle.net/r9HTe/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM