简体   繁体   English

在之后添加带有 css 伪元素的 onclick

[英]Add onclick with css pseudo-element after

Is it possible in my css file to do something like that?:是否可以在我的 css 文件中做类似的事情?:

.myclass:after{
   content:"click me";
   onclick:"my_function()";
 }

I want to add after all instances of myclass a clickable text, in the css style sheet.我想在myclass的所有实例之后在 css 样式表中添加一个可点击的文本。

Is it possible in my css file to do something like [see code above]?是否可以在我的 css 文件中执行类似 [参见上面的代码] 的操作?

No

The important question to ask is why .要问的重要问题是为什么

HTML has control of the data within the webpage. HTML可以控制网页中的数据 Any CSS or JS is specified via the HTML.任何 CSS 或 JS 都是通过 HTML 指定的。 It's the Model .这是模型

CSS has control of the styles , there is no link between CSS and HTML or JavaScript. CSS可以控制样式,CSS 和 HTML 或 JavaScript 之间没有联系。 It's the View .这是视图

JavaScript has control of the interactions within the webpage, and has hooks to any and all DOM nodes. JavaScript可以控制网页内的交互,并具有与任何和所有 DOM 节点的挂钩。 It's the Controller .这是控制器

Because of this MVC structure: HTML belongs in .html files, CSS belongs in .css files, and JS belongs in .js files.因为这种MVC结构:HTML属于.html文件,CSS属于.css文件,JS属于.js文件。

CSS pseudo-elements do not create DOM nodes. CSS 伪元素不会创建 DOM 节点。 There is no direct way for JavaScript to access a pseudo-element defined in CSS, and there's no way to attach an event to said pseudo-elements. JavaScript 无法直接访问 CSS 中定义的伪元素,也无法将事件附加到所述伪元素。

If you've got a set structure in place, and can't add the additional content necessary to produce new links within the HTML, JavaScript can dynamically add the new elements necessary which can then be styled via CSS.如果您有一个固定的结构,并且无法在 HTML 中添加生成新链接所需的额外内容,JavaScript 可以动态添加必要的新元素,然后可以通过 CSS 设置样式。

jQuery makes this very simple: jQuery 使这变得非常简单:

$('<span class="click-me">click me</span>').appendTo('.myclass').click(my_function);

Well,好,

using expression it might actually be possible for internet explorer only.使用表达式实际上可能仅适用于 Internet Explorer。

Otherwise:除此以外:

No. Not at all, that's not what css is made and intended for.不。完全不是,这不是 css 的目的和目的。

If something can be done with jQuery, then it is sure that it is possible to do it without that.如果可以使用 jQuery 完成某些事情,那么可以肯定的是,没有它也可以做到。 Lets see a data model:让我们看一个数据模型:

<div id="container">
<div id="hasblock" onclick='clickFunc(this, event)'></div>
</div>

We need some stylesheet:我们需要一些样式表:

<style>
div#container { width:100px; height:50px;position relative; border:none;}
div#hasblock {width:100px; height:50px;position absolute;border:solid black;}
div#hasblock::after {content:"Click me"; position: absolute; border:solid red;
top:80px;left:0px;display:block;width:100px;height:20px; font-size:18px;}
</style>

And a code:和一个代码:

<script>
function clickFunc(his, event)
{if (event.clientY>his.clientHeight){console.log("It was a click");};    }
</script>

Is it possible in my css file to do something like that?:是否可以在我的css文件中执行类似的操作?:

.myclass:after{
   content:"click me";
   onclick:"my_function()";
 }

I want to add after all instances of myclass a clickable text, in the css style sheet.我想在css样式表中的myclass的所有实例之后添加一个可单击的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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