html仿原生城市选择器,原生js实现html手机端城市列表索引选择城市

本文实例为大家分享了js实现手机端城市列表索引选择城市的具体代码,供大家参考,具体内容如下

html部分:

定位城市
上海市

css部分:

*{

margin: 0;

padding: 0;

list-style: none;

}

html{

font-size: 12px;

}

body {

background-color: #f5f5f5;

font-family: 'PingFang SC Regular', 'PingFang SC';

width: 100%;

height: 100%;

min-width: 320px;

max-width: 480px;

position: relative;

}

.cityPage {

width: 100%;

height: 100%;

/* border: 1px solid black; */

position: relative;

top: 0;

display: flex;

flex-direction: column;

/* justify-content: center; */

}

.cityContent {

width: 100%;

height: 140px;

/* border: 1px solid black; */

background: #f7f7f9;

position: fixed;

z-index: 9999;

top: 0;

}

.inputBox input {

width: 90%;

height: 30px;

border: 1px solid rgb(215, 215, 215);

outline: none;

background: #fff;

margin-left: 4%;

border-radius: 4px;

padding-left: 4px;

color: #9e9e9e;

font-size: 14px;

margin-bottom: 16px;

margin-top: 14px;

}

.localCity {

color: #333;

font-size: 13px;

font-weight: bold;

margin-left: 4.5%;

margin-bottom: 16px;

}

.cityname {

font-size: 13px;

margin-left: 4.5%;

margin-bottom: 16px;

}

#list {

font-size: 13px;

position: fixed;

height: 100%;

top: 140px;

width: 100%;

overflow: scroll;

font-size: 15px;

/* margin-bottom: 140px; */

/* bottom: 200px; */

}

#list>section {

overflow-y: auto;

height: 100%;

margin-bottom: 140px;

}

#list>section>dl>dt {

background: #f7f7f9;

color: #999;

height: 40px;

line-height: 40px;

padding-left: 15px;

}

#list>section>dl>dd {

color: #333;

line-height: 40px;

padding-left: 15px;

position: relative;

background-color: #fff;

}

#list>section>dl>dd:after {

content: '';

position: absolute;

left: 0;

bottom: 1px;

width: 100%;

height: 1px;

background-color: #c8c7cc;

transform: scaleY(.5);

-webkit-transform: scaleY(.5);

}

#list>section>dl>dd:last-of-type:after {

display: none;

}

#navBar {

position: fixed;

width: 26px;

height: 50%;

right: 0;

z-index: 30;

top: 50%;

display: flex;

flex-direction: column;

margin-top: -25%;

/* text-align: center; */

}

#navBar.active {

background: rgba(211, 211, 211, .6);

}

#navBar>div {

text-align: center;

display: block;

text-decoration: none;

/* height: 4.166%;

line-height: 100%; */

color: #333;

font-size: 13px;

flex: 1;

}

.letterBox{

width: 40px;

height: 40px;

background:#9f9f9f;

opacity: .5;

position: fixed;

top: 50%;

left: 50%;

margin-top: -25px;

margin-left: -25px;

text-align: center;

line-height: 40px;

color: #fff;

display: none;

}

js部分:

$(function () {

initCities(cityData);

clickAction()

//输入城市查询

var key = false;

$('#findcityInp').on('compositionstart', function () {

key = true;

console.log('不搜索')

});

$('#findcityInp').on('compositionend', function (e) {

var keyWord = $.trim(e.target.value);

if(keyWord.length>0){

var result = findCity(keyWord, cityData);

initCities(result);

bindEvent();

}else{

initCities(cityData);

bindEvent();

}

});

$('#findcityInp').on('change', function (e) {

var keyWord = $.trim(e.target.value);

console.log(keyWord)

var result = findCity(keyWord, cityData);

// console.log(result)

initCities(result)

});

//城市查询

function findCity(keyWord, data) {

if (!(data instanceof Array)) return;

var reg = new RegExp(keyWord);

var arr = [];

var obj ={

name:'',

cities:[]

}

if(keyWord.length>0 && checkCh(keyWord)==false){

data.forEach((item, index) => {

item.cities.forEach((childItem, childIndex) => {

if (childItem.tags.match(reg)) {

obj.name = childItem.tags[0];

obj.cities.push(childItem)

arr=[obj]

}

})

})

}else if(keyWord.length ==1 && checkCh(keyWord)==true){

data.forEach((item,index)=>{

if(item.name == keyWord){

// console.log(item)

arr.push(item)

}

})

}

else{

arr = data

}

return arr;

}

function checkCh(str){

var RegExp = /^[a-zA-Z]{1}$/;

return RegExp.test(str);

}

//点击右边描点

function toTarget(tag){

var text = $(tag).text();

location.href = "#"+text;

$('.letterBox').html(text);

$('.letterBox').show()

setTimeout(function(){

$('.letterBox').hide()

},1000)

}

//初始化城市列表

function initCities(cityData) {

var g = "";

$('section').html('');

$('nav').html('')

cityData.forEach((item, index) => {

g += "

" + item.name + "

item.cities.forEach((citiesItem, citiesIndex) => {

g += "

" + citiesItem.name + ""

})

g += "

"

})

$('section').append(g);

var g = $('nav').height() / 26;

var f = '';

cityData.forEach((item, index) => {

// f += '' + item.name + ""

f+=`

${item.name}
`

})

$('nav').append(f);

}

//点击城市列表某城市

function clickAction(){

$('.list').click(function (e) {

console.log(e.target.getAttribute('data-name'))

})

}

48efd01f4062dd0e9444de8b73cec590.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设你使用的是原生的时间选择器,可以通过以下步骤来实现点击确认按钮后显示对应的单元格: 1. 首先,在 HTML 中添加一个时间选择器和一个按钮: ```html <input type="datetime-local" id="myDatetime"> <button id="confirmBtn">确认</button> ``` 2. 然后,在 JavaScript 中获取时间选择器和按钮,并给按钮添加点击事件监听器: ```javascript const datetimeInput = document.getElementById('myDatetime'); const confirmBtn = document.getElementById('confirmBtn'); confirmBtn.addEventListener('click', () => { // 在这里处理点击事件 }); ``` 3. 在点击事件处理函数中,获取选择的日期时间,并根据日期时间计算出对应的单元格: ```javascript confirmBtn.addEventListener('click', () => { const selectedDate = new Date(datetimeInput.value); const year = selectedDate.getFullYear(); const month = selectedDate.getMonth() + 1; const day = selectedDate.getDate(); // 根据年月日计算出对应的单元格 const cellIndex = calculateCellIndex(year, month, day); // 在这里显示对应的单元格 }); ``` 4. 最后,根据计算出的单元格索引,显示对应的单元格: ```javascript function calculateCellIndex(year, month, day) { // 在这里计算出对应的单元格索引 // ... return cellIndex; } confirmBtn.addEventListener('click', () => { const selectedDate = new Date(datetimeInput.value); const year = selectedDate.getFullYear(); const month = selectedDate.getMonth() + 1; const day = selectedDate.getDate(); const cellIndex = calculateCellIndex(year, month, day); const cellElement = document.getElementById(`cell${cellIndex}`); cellElement.style.display = 'block'; // 显示对应的单元格 }); ``` 这里的 `cellIndex` 是你根据年月日计算出来的单元格索引,你需要根据你的实际需求来计算。另外,这里假设每个单元格都有一个唯一的 ID,例如 `cell1`、`cell2` 等等,你需要根据你的实际情况来命名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值