li中写一个html的下拉框
时间: 2023-07-30 18:09:07 浏览: 102
下面是一个简单的 HTML 下拉框示例:
```html
<label for="cars">选择一辆汽车:</label>
<select id="cars" name="cars">
<option value="volvo">沃尔沃</option>
<option value="saab">萨博</option>
<option value="mercedes">奔驰</option>
<option value="audi">奥迪</option>
</select>
```
其中,`label` 标签用于描述下拉框的用途,`select` 标签用于创建下拉框,`option` 标签用于定义下拉框中的选项。`id` 属性指定下拉框的 ID,`name` 属性指定表单提交时的名称,`value` 属性指定选项的值。
相关问题
用 JS CSS HTML 帮我写一个百度下拉框
好的,以下是一个简单的百度下拉框示例:
HTML代码:
```
<div class="search-box">
<input type="text" id="search-input" placeholder="请输入搜索关键词">
<ul id="search-list"></ul>
</div>
```
CSS代码:
```
.search-box {
position: relative;
}
#search-input {
width: 300px;
height: 40px;
padding: 10px;
box-sizing: border-box;
border: 1px solid #ccc;
}
#search-list {
position: absolute;
top: 50px;
left: 0;
width: 300px;
max-height: 200px;
overflow-y: auto;
background-color: #fff;
border: 1px solid #ccc;
border-top: none;
list-style: none;
padding: 0;
margin: 0;
display: none;
}
#search-list li {
padding: 5px 10px;
cursor: pointer;
}
#search-list li:hover {
background-color: #f5f5f5;
}
```
JS代码:
```
const searchInput = document.getElementById('search-input');
const searchList = document.getElementById('search-list');
searchInput.addEventListener('input', function() {
const keyword = this.value.trim();
if (keyword) {
const script = document.createElement('script');
script.src = `https://www.baidu.com/su?wd=${keyword}&cb=handleData`;
document.body.appendChild(script);
} else {
searchList.innerHTML = '';
searchList.style.display = 'none';
}
});
function handleData(data) {
const listData = data.s;
let html = '';
for (let i = 0; i < listData.length; i++) {
html += `<li>${listData[i]}</li>`;
}
searchList.innerHTML = html;
searchList.style.display = 'block';
}
document.addEventListener('click', function(e) {
if (e.target !== searchInput) {
searchList.style.display = 'none';
}
});
```
这段代码会监听输入框的输入事件,当输入框中有内容时,会通过动态创建 script 标签的方式向百度搜索接口发送请求,获取搜索建议数据,然后将数据渲染到下拉框中。当输入框中没有内容或者点击页面其他位置时,下拉框会隐藏。
写一个树形组件的下拉框
以下是一个简单的树形下拉框组件,使用了递归的方式生成树形结构:
```html
<template>
<div class="tree-dropdown">
<div class="selected" @click="toggleDropdown">{{ selectedText }}</div>
<ul v-show="isDropdownOpen" class="dropdown">
<li v-for="item in items" :key="item.id" :class="{ 'has-children': item.children }">
<div class="item" @click="selectItem(item)">
<span>{{ item.text }}</span>
<span v-if="item.children" class="toggle-icon" @click="toggleChildren(item)">
{{ item.expanded ? '-' : '+' }}
</span>
</div>
<tree-dropdown v-if="item.children" :items="item.children" :selected-id="selectedId" @select="onSelect"></tree-dropdown>
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'TreeDropdown',
props: {
items: {
type: Array,
required: true,
},
selectedId: {
type: Number,
default: -1,
},
},
data() {
return {
isDropdownOpen: false,
};
},
computed: {
selectedItem() {
return this.items.find(item => item.id === this.selectedId);
},
selectedText() {
return this.selectedItem ? this.selectedItem.text : 'Select an item';
},
},
methods: {
toggleDropdown() {
this.isDropdownOpen = !this.isDropdownOpen;
},
selectItem(item) {
this.$emit('select', item.id);
this.isDropdownOpen = false;
},
toggleChildren(item) {
item.expanded = !item.expanded;
},
onSelect(id) {
this.$emit('select', id);
},
},
};
</script>
<style scoped>
.tree-dropdown {
position: relative;
width: 200px;
}
.selected {
border: 1px solid #ccc;
padding: 5px;
cursor: pointer;
}
.dropdown {
position: absolute;
top: 100%;
left: 0;
z-index: 1;
background-color: #fff;
border: 1px solid #ccc;
padding: 5px;
list-style: none;
margin: 0;
}
.item {
display: flex;
align-items: center;
justify-content: space-between;
cursor: pointer;
}
.toggle-icon {
font-weight: bold;
margin-left: 5px;
}
.has-children .toggle-icon:before {
content: '';
display: inline-block;
width: 0;
height: 0;
border-style: solid;
border-width: 4px 0 4px 6px;
border-color: transparent transparent transparent #000;
margin-right: 5px;
}
.has-children.expanded .toggle-icon:before {
transform: rotate(90deg);
}
</style>
```
使用方式:
```html
<tree-dropdown :items="items" :selected-id="selectedItemId" @select="onItemSelect"></tree-dropdown>
```
其中,`items` 是一个数组,用于表示树形结构,每个元素包含 `id`、`text` 和 `children` 字段。`selectedItemId` 表示当前选中的项的 `id`,`onItemSelect` 是选中项后的回调函数。
阅读全文