javascript - mdl-stepper Stepper.next() 抛出 TypeError
问题描述
我想将 MDL Stepper 与 Material Design Lite 一起使用。
HTML:
<ul class="mdl-stepper mdl-stepper--linear mdl-stepper--horizontal" id="demo-stepper-linear">
<li class="mdl-step">
<span class="mdl-step__label">
<span class="mdl-step__title">
<span class="mdl-step__title-text">Title of step 1</span>
</span>
</span>
<div class="mdl-step__content">
</div>
<div class="mdl-step__actions">
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--colored mdl-button--raised" data-stepper-next>
Continue
</button>
<button class="mdl-button mdl-js-button mdl-js-ripple-effect" data-stepper-cancel>
Cancel
</button>
</div>
</li>
<li class="mdl-step">
<span class="mdl-step__label">
<span class="mdl-step__title">
<span class="mdl-step__title-text">Title longer than it should be</span>
</span>
</span>
<div class="mdl-step__content"></div>
<div class="mdl-step__actions">
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--colored mdl-button--raised" data-stepper-next>
Continue
</button>
<button class="mdl-button mdl-js-button mdl-js-ripple-effect" data-stepper-cancel>
Cancel
</button>
</div>
</li>
<li class="mdl-step">
<span class="mdl-step__label">
<span class="mdl-step__title">
<span class="mdl-step__title-text">Title of step 3</span>
</span>
</span>
<div class="mdl-step__content"></div>
<div class="mdl-step__actions">
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--colored mdl-button--raised" data-stepper-next>
Continue
</button>
<button class="mdl-button mdl-js-button mdl-js-ripple-effect" data-stepper-cancel>
Cancel
</button>
</div>
</li>
</ul>
JavaScript:
<script type="text/javascript">
var stepperElement = document.querySelector('ul.mdl-stepper');
var Stepper;
// Check if MDL Component Handler is loaded.
if (typeof componentHandler !== 'undefined') {
// Get the MaterialStepper instance of element to control it.
Stepper = stepperElement.MaterialStepper;
// Moves the stepper to the next step for test.
Stepper.next();
} else {
// Material Design Lite javascript is not loaded or for another
// reason MDL Component Handler is not available globally and
// you can't use (register and upgrade) Stepper component at this point.
}
</script>
我无法运行它,因为我遇到了错误...
未捕获的类型错误:无法读取未定义的属性“下一个”
我可以做些什么来使用这个功能?
解决方案
可能是您的 js 在 DOM 完成加载之前正在运行 用下面的代码包装您的代码,它会等到 dom 完成
document.onreadystatechange = function () {
if (document.readyState == "interactive") {
var stepperElement = document.querySelector('ul.mdl-stepper'); var Stepper;
// Check if MDL Component Handler is loaded.
if (typeof componentHandler !== 'undefined') {
// Get the MaterialStepper instance of element to control it.
Stepper = stepperElement.MaterialStepper;
// Moves the stepper to the next step for test.
Stepper.next();
} else {
// Material Design Lite javascript is not loaded or for another
// reason MDL Component Handler is not available globally and
// you can't use (register and upgrade) Stepper component at this point.
}
}
}
推荐阅读
- r - 识别 xml 文件的唯一属性
- python - 为什么 numpy 数组列数据类型没有更新?
- c++ - 使用线程的矩阵求逆速度较慢
- php - 为什么 array_filter() 在迭代时不改变我的字符串?
- java - 如何以编程方式将 sup 编号添加到 TextView?
- python - Keras:训练期间和 epoch 结束时的不同验证 AUROC
- android - 里面有圆角和圆角图像的长条叫什么名字?
- python - 这个 PyCharm 警告对我来说没有意义
- azure - Azure Blob 存储 - 发送图像名称列表并获取图像列表
- ios - 我的天气数据没有显示在我的视图控制器中