使用 CSS 伪元素,插入具有点击处理程序的内容

发布于 2024-12-04 17:12:07 字数 235 浏览 1 评论 0原文

我正在做一些非常简单的事情,如下所示:

<div class='item'>HI I'm an item</div>

.item:hover:after {
content: "Delete";
}

我想做的是当用户将鼠标悬停在 div 上时,出现“删除”一词,并且当用户单击“删除”一词时,我可以运行一些jQuery(实际删除此项)

这可能吗?

I'm doing something very simple, like this:

<div class='item'>HI I'm an item</div>

.item:hover:after {
content: "Delete";
}

What i want to do is when the user hovers over the div, for the word Delete to appear and have it possible that when the user clicks on the word Delete, for me to run some jQuery (to actually remove this item)

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱娆 2024-12-11 17:12:07

那是不可能的,抱歉。伪元素纯粹用于演示目的。

是的,我只是想避免带有跨度等的额外标记...

对于像“删除”链接这样重要的东西,正确的位置是在 HTML 中。


如果你真的不想在 HTML 中这样做(也许你有情有可原的情况),你不妨使用更多的 jQuery 来做到这一点:

$('<span class="del">Delete</span>').appendTo('.item').click(function() {
    alert('deleted');
});

.del {
    display: none;
}
.item:hover .del {
    display: inline;
}

That's just not possible, sorry. Pseudo-elements are purely for presentation purposes.

Yes, i just wanted to avoid extra markup with spans etc...

For something as important as a "Delete" link, the proper place for this to go is in the HTML.


If you really don't want this in the HTML (maybe you have extenuating circumstances), you might as well just do it using a little more jQuery:

$('<span class="del">Delete</span>').appendTo('.item').click(function() {
    alert('deleted');
});

.del {
    display: none;
}
.item:hover .del {
    display: inline;
}
话少情深 2024-12-11 17:12:07

那是不可能的。您可以通过以下方式实现所需的结果:
CSS:

.item > .del {
    /* "A > B" means: Select element B which is the direct child of A.
     * You surely don't want all nested .del elements to collapse. */
    width: 100px;
    height: 30px;
    display: none;
    /*Additional styles*/
}
.item:hover > .del{
    display:inline-block;
}

HTML:

<div class="item">
   Blabla
   <div class="del" onclick="doSomething()">Delete</div>
</div>

That's not possible. You can accomplish the desired results in this way:
CSS:

.item > .del {
    /* "A > B" means: Select element B which is the direct child of A.
     * You surely don't want all nested .del elements to collapse. */
    width: 100px;
    height: 30px;
    display: none;
    /*Additional styles*/
}
.item:hover > .del{
    display:inline-block;
}

HTML:

<div class="item">
   Blabla
   <div class="del" onclick="doSomething()">Delete</div>
</div>
没企图 2024-12-11 17:12:07

你完全可以做到这一点,尽管我不确定“删除”......你的意思是只是将其从 DOM 中取出吗?如果是这样,这就是您的解决方案。
http://jsfiddle.net/jalbertbowdenii/UVxWq/

好的,这是适合您的规范的解决方案:< a href="http://jsfiddle.net/jalbertbowdenii/UVxWq/" rel="nofollow">http://jsfiddle.net/jalbertbowdenii/UVxWq/ 鼠标悬停时会显示删除,onclick 会删除 .item。没有额外的标记。

you can totally do that, although i'm not sure about "delete"....do you mean just take it out of the DOM? if so, here's your solution.
http://jsfiddle.net/jalbertbowdenii/UVxWq/

ok, here's the solution for your specification: http://jsfiddle.net/jalbertbowdenii/UVxWq/ delete shows up on hover and onclick deletes the .item. no extra markup.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文