最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to show edit action icon when mouse-over on particular text - Stack Overflow

programmeradmin3浏览0评论

How to show / hide edit icon on mouseover and mouseout on particular text.

Here is my html code snippet

<ul>
    <li>
        <a id="pop" href="javascript:;;" data-content="test Desc" data-id="123">
            <span class="testNameInfo">Test</span>
        </a>
        <div class="pull-right icons-align">
            <a href="javascript:;;" class="editInline"><i class="fa fa-pencil"></i>..</a>
            <a href="javascript:;;"><i class="fa fa-plus"></i></a>
            <a href="javascript:;;"><i class="fa fa-times"></i></a>
        </div>
    </li>
</ul>

when page loads the fa-pencil icon is in hide state. When i mouse over on text, it should show fa-pencil icon. Remaining icons (add and delete) are always in show state.

Here is my javascript to hide the fa-pencil icon

$("a.editInline").css("display","none");

Am using backbone and marionette js frameworks, so i need to register the events in view.

Please let me know what is the best way to get out from my issue.

How to show / hide edit icon on mouseover and mouseout on particular text.

Here is my html code snippet

<ul>
    <li>
        <a id="pop" href="javascript:;;" data-content="test Desc" data-id="123">
            <span class="testNameInfo">Test</span>
        </a>
        <div class="pull-right icons-align">
            <a href="javascript:;;" class="editInline"><i class="fa fa-pencil"></i>..</a>
            <a href="javascript:;;"><i class="fa fa-plus"></i></a>
            <a href="javascript:;;"><i class="fa fa-times"></i></a>
        </div>
    </li>
</ul>

when page loads the fa-pencil icon is in hide state. When i mouse over on text, it should show fa-pencil icon. Remaining icons (add and delete) are always in show state.

Here is my javascript to hide the fa-pencil icon

$("a.editInline").css("display","none");

Am using backbone and marionette js frameworks, so i need to register the events in view.

Please let me know what is the best way to get out from my issue.

Share Improve this question asked Jul 1, 2015 at 13:28 NaniGNaniG 1191 silver badge12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You can do as below:

$('.testNameInfo').on('mouseover mouseout',function(){
     $(this).closest('li').find('.editInline').toggle();
     //find the closest li and find its children with class editInLine and 
     //toggle its display using 'toggle()'
});

UPDATE

DEMO

@JamieBarker made his point which is valid here so I would suggest to try below code instead

$("a.editInline").css("display","none");
$('li').on('mouseover mouseout',function(){
     $(this).find('.editInline').toggle();
     //find its children with class .editInLine and 
     //toggle its display using 'toggle()'
});

Better to use CSS than JavaScript if you can:

a.editInline {
    display:none;
}
li:hover a.editInline {
    display:inline-block;
}

UPDATE

Performance/Simplicity wise I'd advise going with the CSS solution provided. If all else you can use then JS solution.

Optional CSS Solution

.editInline {
  display: none;
}

#pop:hover .icons-align .editInline {
 display: inline-block;
}

JS Solution

$(function() {
  $(".editInline").hide(); // Use this if CSS is not wanted

  $("#pop").hover(function() {
    $(".editInline").show();
  }, function() {
    $(".editInline").hide();
  });
});  
发布评论

评论列表(0)

  1. 暂无评论