用layui写一个特殊定制页面,可以有好几个板块,有购买按钮,可以让我插入图片,
时间: 2023-09-29 15:06:38 浏览: 108
好的,以下是一个示例代码,使用了Layui的Tab选项卡和购买按钮组件,并且允许插入图片。你可以根据自己的需求进行修改。
HTML代码:
```html
<div class="layui-tab layui-tab-card">
<ul class="layui-tab-title">
<li class="layui-this">板块一</li>
<li>板块二</li>
<li>板块三</li>
</ul>
<div class="layui-tab-content">
<div class="layui-tab-item layui-show">
<div class="layui-card">
<div class="layui-card-header">板块一</div>
<div class="layui-card-body">
<img src="" alt="" id="image1">
<button class="layui-btn layui-btn-normal" id="buyBtn1">购买</button>
</div>
</div>
</div>
<div class="layui-tab-item">
<div class="layui-card">
<div class="layui-card-header">板块二</div>
<div class="layui-card-body">
<img src="" alt="" id="image2">
<button class="layui-btn layui-btn-normal" id="buyBtn2">购买</button>
</div>
</div>
</div>
<div class="layui-tab-item">
<div class="layui-card">
<div class="layui-card-header">板块三</div>
<div class="layui-card-body">
<img src="" alt="" id="image3">
<button class="layui-btn layui-btn-normal" id="buyBtn3">购买</button>
</div>
</div>
</div>
</div>
</div>
<!--插入图片的弹窗-->
<div class="layui-layer layui-layer-page" id="imageLayer" style="display:none;">
<div class="layui-layer-title">插入图片</div>
<div class="layui-layer-content">
<form class="layui-form" action="">
<div class="layui-form-item">
<label class="layui-form-label">图片链接</label>
<div class="layui-input-block">
<input type="text" name="imageLink" lay-verify="required" placeholder="请输入图片链接" autocomplete="off"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn layui-btn-normal" lay-submit lay-filter="submitImage">确定</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
</div>
</div>
```
JavaScript代码:
```javascript
// 初始化Tab组件
layui.use('element', function() {
var element = layui.element;
// 监听Tab切换事件,加载图片
element.on('tab', function(data) {
var imgId = "image" + (data.index + 1);
var img = document.getElementById(imgId);
if (img.src === "") {
layer.open({
type: 1,
title: '插入图片',
content: $('#imageLayer'),
area: ['500px', '200px'],
btn: [],
success: function(layero, index) {
// 监听图片插入表单的提交事件
layui.form.on('submit(submitImage)', function(formData) {
var imageLink = formData.field.imageLink;
img.src = imageLink;
layer.close(index);
return false; // 阻止表单跳转
});
}
});
}
});
});
// 监听购买按钮的点击事件
document.getElementById("buyBtn1").addEventListener("click", function() {
alert("购买成功!");
});
document.getElementById("buyBtn2").addEventListener("click", function() {
alert("购买成功!");
});
document.getElementById("buyBtn3").addEventListener("click", function() {
alert("购买成功!");
});
```
CSS代码:
```css
.layui-card {
margin-top: 20px;
}
```
阅读全文