.change()和.toggle()的非JQuery版本是什么?
What are the non JQuery versions of .change() and .toggle()?
如何将此代码的JQuery版本转换为非JQuery?
JQueryjQuery(function($){
//change event handler for the checkbox
$('#isTracefile').change(function(){
//if it is checked set the select element's container to display else hide
$('#cttracefile').toggle(this.checked);
//if it is unchecked set the input element's container to display else hide
$('#custom').toggle(!this.checked);
}).change()
})
<div id="cttracefile">
Sample Tracefiles to use:
<select name="tracefile">
<option value="capacity.13Mbps_200RTT_PER_0.000001.txt">capacity.13Mbps_200RTT_PER_0.000001</option>
</select>
</div>
<div id="custom">
<label>Forward Delay: </label><input id="fd" type="number" min="0" max="1000" step="1" value ="100"/> ms
<br/>
<label>Reverse Delay: </label><input id="rd" type="number" min="0" max="1000" step="1" value ="100"/> ms
<br/>
<label>Download Capacity: </label><input id="down" type="number" min="1" max="30" step="1" value ="1"/> MBps
<br/>
<label>Upload Capacity: </label><input id="up" type="number" min="1" max="30" step="1" value ="1"/> MBps
<br/>
<label>Packet Error Rate (PER): </label><input id="per" type="number" min="0.00001" max="1" step="0.00001" value ="0.00001"/>
</div>
.change()
和.toggle()
的非jquery版本是什么
这可以用作非jquery的解决方案。你只需要设置初始值
var isTrace = document.getElementById('isTracefile');
var ctTrace = document.getElementById('cttracefile');
var custom = document.getElementById('custom');
isTrace.onchange = function(){
if (isTrace.checked) {
ctTrace.style.display = "none";
custom.style.display = "block";
} else {
custom.style.display = "none";
ctTrace.style.display = "block";
}
};
演示小提琴:http://jsfiddle.net/adjit/3at2c/4/
-
$(fn)
变为window.addEventListener('load', fn)
-
$('#id')
变为document.getElementById('id')
-
$jqo.change(fn)
变为element.addEventListener('change', fn)
- 基于bool
element.style.display = bool ? '' : 'none'
更改可见性
最后,在节点上触发change
事件,这在vanilla中要做得更复杂一些,具体取决于您想要支持的浏览器,但是您可以使用element.dispatchEvent
和new Event(type)
。
所以全部加起来,
window.addEventListener('load', function () {
document.getElementById('isTracefile')
.addEventListener('change', function () {
document.getElementById('cttracefile')
.style.display = this.checked ? '' : 'none';
document.getElementById('custom')
.style.display = !this.checked ? '' : 'none';
});
document.getElementById('isTracefile').dispatchEvent(new Event('change'));
});
相关文章:
- 这是什么 ==- javascript 运算符
- 我的单元测试选项是什么
- 打破承诺链的好方法是什么
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- Javascript中的空白是什么
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- 当使用browserfy时,建议在生产中要求缩小版本,但在开发中进行调试的方法是什么
- 如何将 phonegap 2.5 的插件升级到最新版本 3.1?以及向PhoneGap添加额外插件的流程是什么
- Firebase的版本是什么https://cdn.firebase.com/v0/firebase.js.
- jQuery' $var = ($var)语句的javascript版本是什么?
- .change()和.toggle()的非JQuery版本是什么?
- firefox浏览器(所有版本)中的.load()等效函数是什么?
- 与原始版本相反,任何框架或任何人都需要使用String/Number/Boollean对象的原因是什么
- ES6、ES7(及更大版本)、ES2015、阶段0、阶段1(及更大版本)之间的关系是什么?
- Java' Rhino实现实现了哪个JavaScript (ECMAScript)版本(更新策略是什么?)
- 云服务器的版本和库支持是什么?
- 哪个版本的Chrome支持window.performance.clearResourceTimings,替代方案是什么
- 在PrimeFaces 5.2及更新版本中,图表扩展器属性的替代方法是什么?
- 我的主干版本是什么?
- 使用Semantic-UI的最小JQuery版本是什么?