在模板中调用jquery库
修改一下模板代码,添加一个按钮,并编写js代码,调用click方法:
<?php
/** \ExampleCorp\Learning\Block\Widget\Test $block */
$bannerImg = $this->getViewFileUrl('Lyh_Banner::images/banners/banner.png');
?>
<style>
#banner-img{
width:100%;
height:300px;
}
</style>
<h3>这个模板来自Summer-green主题</h3>
<div class="banner-container">
<img id='banner-img' src="<?=$bannerImg?>">
</div>
<div>
<button id='btn'>点击事件</button>
</div>
<script>
require([
'jquery'
], function($) {
$("#btn").click(function(){
alert('按钮点击事件被触发了');
})
})
</script>
回到首页,点击按钮,按钮事件被触发了。
码小课更多关于magento2的学习资料: