- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将此 jQuery 代码段转换为普通 JavaScript,但我似乎在 focus 和 blur 事件方面遇到问题。
我似乎无法让我的回调函数在这些事件上执行。
为什么 jQuery 代码片段有效,而我的却无效?
HTML 代码段
form(class="form js-form" method="post" action="/contact")
div.row
div.col-sm-12
div.form__group.js-form-group
label(class="form__label js-form-label" for="name") Name
input(class="form__input js-form-input" type="text" name="name" id="name" placeholder="Name" required)
div.row
div.col-sm-12
div.form__group.js-form-group
label(class="form__label js-form-label" for="email") Email
input(class="form__input js-form-input" type="email" name="email" id="email" placeholder="Email" required)
div.row
div.col-sm-6
div.form__group.js-form-group
label(class="form__label js-form-label" for="occasion") Occasion
input(class="form__input js-form-input" type="text" name="occasion" id="occasion" placeholder="Occasion" required)
div.col-sm-6
div.form__group.js-form-group
label(class="form__label js-form-label" for="date") Date
input(class="form__input js-form-input" type="date" name="date" id="date" placeholder="Date" required)
div.row
div.col-sm-12
div.form__group.js-form-group
label(class="form__label js-form-label" for="message")
textarea(class="form__textarea js-form-input" name="message" id="message" placeholder="Message" required)
div.row
div.col-sm-12
button.pull-right(class="form__submit js-form-submit" type="submit") Submit
jQuery 代码片段
$(function() {
$(".js-form").on("input propertychange", ".js-form-group", function(e) {
$(this).toggleClass("has-value", !!$(e.target).val());
console.log(this);
}).on("focus", ".js-form-group", function() {
$(this).addClass("focused");
console.log('focus');
}).on("blur", ".js-form-group", function() {
$(this).removeClass("focused");
console.log("blur");
});
});
原版代码片段不起作用
var formFloatingLabels = (function() {
var formSelector = 'js-form',
formGroupSelector = 'js-form-group',
form = document.getElementsByClassName(formSelector)[0],
formGroup = null,
focusedClass = 'focused',
hasValueClass = 'has-value';
if (!form) {
return;
}
formGroup = form.getElementsByClassName(formGroupSelector);
function init() {
bindFormGroupEventListeners();
}
function toggleHasValueClass() {
this.classList.toggle(hasValueClass);
}
function addFocusedClass() {
console.log('focused');
}
function removeFocusedClass() {
console.log('blurred');
}
function bindFormGroupEventListeners() {
var currentFormGroup = null;
for (var i = 0; i < formGroup.length; i++) {
currentFormGroup = formGroup[i];
currentFormGroup.addEventListener('input', toggleHasValueClass);
currentFormGroup.addEventListener('focus', addFocusedClass);
currentFormGroup.addEventListener('blur', removeFocusedClass);
}
}
return {
init: init
}
})();
formFloatingLabels.init();
最佳答案
我不确定为什么 jQuery 代码可以工作。
“焦点”和“模糊”是原生的 <input>
事件。
要使您的代码正常工作,请更改 formGroupSelector = 'js-form-group',
至formGroupSelector = 'js-form-input',
或者将输入标记更改为 js-form-group
类.
这是 jsbin 中代码的一个小型工作副本:
https://jsbin.com/wiwori/2/edit?html,js,console,output
顺便说一句,放弃 jQuery 干得好,这是学习 javascript 的好方法。
更新
我确实知道 jQuery 为何有效。 http://api.jquery.com/on/
The majority of browser events bubble, or propagate, from the deepest, innermost element (the event target) in the document where they occur all the way up to the body and the document element. In Internet Explorer 8 and lower, a few events such as change and submit do not natively bubble but jQuery patches these to bubble and create consistent cross-browser behavior.
还有
The focus and blur events are specified by the W3C to not bubble, but jQuery defines cross-browser focusin and focusout events that do bubble. When focus and blur are used to attach delegated event handlers, jQuery maps the names and delivers them as focusin and focusout respectively. For consistency and clarity, use the bubbling event type names.
简单地说,因为您使用“on”jQuery 将焦点事件冒泡到 div,所以这不是 native 浏览器行为。
关于javascript - 将此 jQuery 片段转换为普通 javascript(焦点不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41649259/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!