使用 CSS 伪元素,插入具有点击处理程序的内容
我正在做一些非常简单的事情,如下所示:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那是不可能的,抱歉。伪元素纯粹用于演示目的。
对于像“删除”链接这样重要的东西,正确的位置是在 HTML 中。
如果你真的不想在 HTML 中这样做(也许你有情有可原的情况),你不妨使用更多的 jQuery 来做到这一点:
That's just not possible, sorry. Pseudo-elements are purely for presentation purposes.
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:
那是不可能的。您可以通过以下方式实现所需的结果:
CSS:
HTML:
That's not possible. You can accomplish the desired results in this way:
CSS:
HTML:
你完全可以做到这一点,尽管我不确定“删除”......你的意思是只是将其从 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.