简体   繁体   English

如何毫不干扰地使用此代码?

[英]How to use this code as Unobtrusively?

How to use this code as Unobtrusively? 如何毫不干扰地使用此代码? To keep content and Behaviour separate. 保持内容和行为分开。

    <a href="nojavascript.html" onclick="functionName(); return false;" 
onkeypress="functionName(); return false;">Link text</a>

You could write it like this: 您可以这样写:

<a href="nojavascript.html">Link text</a>

Then use jQuery's .bind() like this: 然后像这样使用jQuery的.bind()

$("a[href$='nojavascript.html']").bind("click keypress", function() {
  functionName();
  return false;
});

Or alternatively, give it a class like this: 或者,给它一个这样的类:

<a class="noJS" href="nojavascript.html">Link text</a>

And use a .class selector like $("a.noJS") instead to be a bit cleaner (or, if it's unique, use an ID). 并使用$("a.noJS")类的.class选择器代替它,以使其更$("a.noJS") (或者,如果唯一,则使用ID)。

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

相关问题 如何用JS轻松地更新页面标题(在Rails中) - How to unobtrusively update the page title with JS (in Rails) 如何使用JavaScript和CSS轻松地为表单元素设置样式? - How to style form elements unobtrusively with JavaScript and CSS? 如何毫不客气地使用jQuery使可访问弹出窗口? - How to make accessible popup using jquery unobtrusively? 如何在不显眼地连接事件时找到 JavaScript 运行 - How to find JavaScript run when an event is hooked up unobtrusively 如何使用Javascript和Prototype毫不费力地禁用提交按钮? - How can I unobtrusively disable submit buttons with Javascript and Prototype? 如何在Javascript中不显眼地从按钮点击的文本中读取输入? - How to read input from text on button click unobtrusively in Javascript? 您如何轻松地增强jQuery Datepicker类? - How might you unobtrusively enhance the jQuery Datepicker class? 当使用 AJAX 创建元素时,如何不显眼地将 function 绑定到元素的点击 - How to unobtrusively bind a function to an element's click when the element was created with AJAX 毫不费力地将javascript应用于文本框 - Applying javascript to a textbox unobtrusively 角度引导验证毫不费力地完成 - Angular bootstrap validation done unobtrusively
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM