TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Tips for how to decouple your HTML, CSS, and JavaScript

16 点作者 philipwalton将近 12 年前

1 comment

SchizoDuckie将近 12 年前
Why promote the (ab)use of the class attribute for binding user interface behavior?<p>It&#x27;s 2013, people that write blogposts should be directing their readers (imo) to using the (html5) data-* attributes (which works equally simple) and actually removes the declaration and mixture of behavior and styling from your css.<p>For instance your button example:<p><pre><code> &lt;button data-behavior=&#x27;addtocart&#x27; data-somespecialparameter=&#x27;iamspecial&#x27; class=&quot;add-item&quot;&gt;Add to Cart&lt;&#x2F;button&gt; </code></pre> Would behave on some javascript:<p>jQuery:<p><pre><code> $(document.body).on(&#x27;click&#x27; &#x27;button[data-behavior=&quot;addtocart&quot;]&#x27;, myHandlerFunction); </code></pre> MooTools<p><pre><code> document.body.addEvent(&#x27;click:relay(button[data-behavior=&quot;addtocart&quot;])&#x27;, myHandlerFunction); </code></pre> Handler:<p><pre><code> function myHandlerFunction(ev) { console.log(&quot;I just got clicked. &quot;, this.getAttribute(&#x27;data-somespecialparameter&#x27;), this); } </code></pre> Also, using observers on a container element makes sure that you can dynamically add and remove html elements without having to worry about cleaning up the even handlers. (binding it all on document.body or the nearest container can be debatable for speed reasons, but this is up to you)
评论 #6238528 未加载