Home php教程 PHP源码 Multi-city selector implementation code

Multi-city selector implementation code

Dec 28, 2016 pm 01:40 PM
city ​​selector


需求 : 热门城市、列表中的城市都需要在数据库中查询出来后构建列表,并按a-z排列,输入字母可以实现查询。

多城市选择器实现效果图

Multi-city selector implementation code实现:

1 后台根据登录用户获取列表中城市和热门城市的数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@RequestMapping(value = "/getCity.do")

 2     public void getCity(PrintWriter printWriter, HttpServletResponse response)

 3     {

 4         response.setContentType("text/html;charset=UTF-8");

 5         User user = this.getLoginUser();

 6         //查询出系统登陆人所有的所属区域名称

 7       String allCity = getAllCityProperty(user.getId(), "areaName");

 8       String hotCity = getHotCity(allCity);

 9       JSONObject json = new JSONObject();       

10         json.accumulate("hotCity",hotCity);

11         json.accumulate("allCity",allCity);

12         printWriter.print(json.toString());

13         printWriter.flush();

14         printWriter.close();

15     }

Copy after login

2 前台打开界面的时候通过ajax获取城市和热门城市

1

2

3

4

5

6

7

8

9

10

11

12

13

14

1 window.onload=function(){   

 2        var url = "/xxx/getCity.do";

 3          var params = {};

 4          $.getJSON(url,params,function(data){

 5              if(data.length==0)

 6              {

 7                   $.messager.alert('提示',"未找到数据");

 8              }else{

 9                   var hotString = data.hotCity;

10                   var allString = data.allCity;

11                 new Vcity.CitySelector({input:'city'},allString,hotString);

12                 }              

13         });

14 }

Copy after login

3 citys.js 根据百度上的城市选择组件 新增热门城市和城市变量

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

/* *

 * ---------------------------------------- *

 * 城市选择组件 v1.0

 * Author: zcx

 * ---------------------------------------- *

 * Date: 2016-05-17

 * ---------------------------------------- *

 * */

/* *

 * 全局空间 Vcity

 * */

var Vcity = {};

/* *

 * 静态方法集

 * @name _m

 * */

Vcity._m = {

    /* 选择元素 */

    $:function (arg, context) {

        var tagAll, n, eles = [], i, sub = arg.substring(1);

        context = context || document;

        if (typeof arg == 'string') {

            switch (arg.charAt(0)) {

                case '#':

                    return document.getElementById(sub);

                    break;

                case '.':

                    if (context.getElementsByClassName) return context.getElementsByClassName(sub);

                    tagAll = Vcity._m.$('*', context);

                    n = tagAll.length;

                    for (i = 0; i < n; i++) {

                        if (tagAll[i].className.indexOf(sub) > -1) eles.push(tagAll[i]);

                    }

                    return eles;

                    break;

                default:

                    return context.getElementsByTagName(arg);

                    break;

            }

        }

    },

    /* 绑定事件 */

    on:function (node, type, handler) {

        node.addEventListener ? node.addEventListener(type, handler, false) : node.attachEvent(&#39;on&#39; + type, handler);

    },

    /* 获取事件 */

    getEvent:function(event){

        return event || window.event;

    },

    /* 获取事件目标 */

    getTarget:function(event){

        return event.target || event.srcElement;

    },

    /* 获取元素位置 */

    getPos:function (node) {

        var scrollx = document.documentElement.scrollLeft || document.body.scrollLeft,

            scrollt = document.documentElement.scrollTop || document.body.scrollTop;

        var pos = node.getBoundingClientRect();

        return {top:pos.top + scrollt, right:pos.right + scrollx, bottom:pos.bottom + scrollt, left:pos.left + scrollx }

    },

    /* 添加样式名 */

    addClass:function (c, node) {

        if(!node)return;

        node.className = Vcity._m.hasClass(c,node) ? node.className : node.className + &#39; &#39; + c ;

    },

    /* 移除样式名 */

    removeClass:function (c, node) {

        var reg = new RegExp("(^|\\s+)" + c + "(\\s+|$)", "g");

        if(!Vcity._m.hasClass(c,node))return;

        node.className = reg.test(node.className) ? node.className.replace(reg, &#39;&#39;) : node.className;

    },

    /* 是否含有CLASS */

    hasClass:function (c, node) {

        if(!node || !node.className)return false;

        return node.className.indexOf(c)>-1;

    },

    /* 阻止冒泡 */

    stopPropagation:function (event) {

        event = event || window.event;

        event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true;

    },

    /* 去除两端空格 */

    trim:function (str) {

        return str.replace(/^\s+|\s+$/g,&#39;&#39;);

    }

};

/* 正则表达式 筛选中文城市名、拼音、首字母 */

Vcity.regEx = /^([\u4E00-\u9FA5\uf900-\ufa2d]+)\|(\w+)\|(\w)\w*\|(\d+)$/i;

Vcity.regExChiese = /([\u4E00-\u9FA5\uf900-\ufa2d]+)/;

function adapterCity (citylist,hotStr) {

    if(!citylist){

        return;

    }

    var citys = citylist;

    var match;

    var letter;

    var regEx = Vcity.regEx,

        reg2 = /^[a]$/i, reg3 = /^[b]$/i, reg4 = /^[c]$/i;

        reg5 = /^[d]$/i, reg6 = /^[e]$/i, reg7 = /^[f]$/i;

        reg8 = /^[g]$/i, reg9 = /^[h]$/i, reg10 = /^[i]$/i;

        reg11 = /^[j]$/i, reg12 = /^[k]$/i, reg13 = /^[l]$/i;

        reg14 = /^[m]$/i, reg15 = /^[n]$/i, reg16 = /^[o]$/i;

        reg17 = /^[p]$/i, reg18 = /^[q]$/i, reg19 = /^[r]$/i;

        reg20 = /^[s]$/i, reg21 = /^[t]$/i, reg22 = /^[u]$/i;

        reg23 = /^[v]$/i, reg24 = /^[w]$/i, reg25 = /^[x]$/i;

        reg26 = /^[y]$/i, reg27 = /^[z]$/i;

    //if (!Vcity.oCity) {

        Vcity.oCity = {hot:{},A:{},B:{},C:{},D:{},E:{},F:{},G:{},H:{}, I:{},J:{},K:{},L:{},M:{},N:{},O:{},P:{}, Q:{},R:{},S:{},T:{},U:{},V:{},W:{},X:{},Y:{},Z:{}};

        //console.log(citys.length);

        for (var i = 0; i < citys.length; i++) {

            match = regEx.exec(citys[i]);

            letter = match[3].toUpperCase();

            if (reg2.test(letter)) {

                if (!Vcity.oCity.A[letter]) Vcity.oCity.A[letter] = [];

                Vcity.oCity.A[letter].push(match[1]);

            } else if (reg3.test(letter)) {

                if (!Vcity.oCity.B[letter]) Vcity.oCity.B[letter] = [];

                Vcity.oCity.B[letter].push(match[1]);

            } else if (reg4.test(letter)) {

                if (!Vcity.oCity.C[letter]) Vcity.oCity.C[letter] = [];

                Vcity.oCity.C[letter].push(match[1]);

            }else if (reg5.test(letter)) {

                if (!Vcity.oCity.D[letter]) Vcity.oCity.D[letter] = [];

                Vcity.oCity.D[letter].push(match[1]);

            }else if (reg6.test(letter)) {

                if (!Vcity.oCity.E[letter]) Vcity.oCity.E[letter] = [];

                Vcity.oCity.E[letter].push(match[1]);

            }else if (reg7.test(letter)) {

                if (!Vcity.oCity.F[letter]) Vcity.oCity.F[letter] = [];

                Vcity.oCity.F[letter].push(match[1]);

            }else if (reg8.test(letter)) {

                if (!Vcity.oCity.G[letter]) Vcity.oCity.G[letter] = [];

                Vcity.oCity.G[letter].push(match[1]);

            }else if (reg9.test(letter)) {

                if (!Vcity.oCity.H[letter]) Vcity.oCity.H[letter] = [];

                Vcity.oCity.H[letter].push(match[1]);

            }else if (reg10.test(letter)) {

                if (!Vcity.oCity.I[letter]) Vcity.oCity.I[letter] = [];

                Vcity.oCity.I[letter].push(match[1]);

            }else if (reg11.test(letter)) {

                if (!Vcity.oCity.J[letter]) Vcity.oCity.J[letter] = [];

                Vcity.oCity.J[letter].push(match[1]);

            }else if (reg12.test(letter)) {

                if (!Vcity.oCity.K[letter]) Vcity.oCity.K[letter] = [];

                Vcity.oCity.K[letter].push(match[1]);

            }else if (reg13.test(letter)) {

                if (!Vcity.oCity.L[letter]) Vcity.oCity.L[letter] = [];

                Vcity.oCity.L[letter].push(match[1]);

            }else if (reg14.test(letter)) {

                if (!Vcity.oCity.M[letter]) Vcity.oCity.M[letter] = [];

                Vcity.oCity.M[letter].push(match[1]);

            }else if (reg15.test(letter)) {

                if (!Vcity.oCity.N[letter]) Vcity.oCity.N[letter] = [];

                Vcity.oCity.N[letter].push(match[1]);

            }else if (reg16.test(letter)) {

                if (!Vcity.oCity.O[letter]) Vcity.oCity.O[letter] = [];

                Vcity.oCity.O[letter].push(match[1]);

            }else if (reg17.test(letter)) {

                if (!Vcity.oCity.P[letter]) Vcity.oCity.P[letter] = [];

                Vcity.oCity.P[letter].push(match[1]);

            }else if (reg18.test(letter)) {

                if (!Vcity.oCity.Q[letter]) Vcity.oCity.Q[letter] = [];

                Vcity.oCity.Q[letter].push(match[1]);

            }else if (reg19.test(letter)) {

                if (!Vcity.oCity.R[letter]) Vcity.oCity.R[letter] = [];

                Vcity.oCity.R[letter].push(match[1]);

            }else if (reg20.test(letter)) {

                if (!Vcity.oCity.S[letter]) Vcity.oCity.S[letter] = [];

                Vcity.oCity.S[letter].push(match[1]);

            }else if (reg21.test(letter)) {

                if (!Vcity.oCity.T[letter]) Vcity.oCity.T[letter] = [];

                Vcity.oCity.T[letter].push(match[1]);

            }else if (reg22.test(letter)) {

                if (!Vcity.oCity.U[letter]) Vcity.oCity.U[letter] = [];

                Vcity.oCity.U[letter].push(match[1]);

            }else if (reg23.test(letter)) {

                if (!Vcity.oCity.V[letter]) Vcity.oCity.V[letter] = [];

                Vcity.oCity.V[letter].push(match[1]);

            }else if (reg24.test(letter)) {

                if (!Vcity.oCity.W[letter]) Vcity.oCity.W[letter] = [];

                Vcity.oCity.W[letter].push(match[1]);

            }else if (reg25.test(letter)) {

                if (!Vcity.oCity.X[letter]) Vcity.oCity.X[letter] = [];

                Vcity.oCity.X[letter].push(match[1]);

            }else if (reg26.test(letter)) {

                if (!Vcity.oCity.Y[letter]) Vcity.oCity.Y[letter] = [];

                Vcity.oCity.Y[letter].push(match[1]);

            }else if (reg27.test(letter)) {

                if (!Vcity.oCity.Z[letter]) Vcity.oCity.Z[letter] = [];

                Vcity.oCity.Z[letter].push(match[1]);

            }

            /* 热门城市 */

                if(!Vcity.oCity.hot[&#39;hot&#39;]) Vcity.oCity.hot[&#39;hot&#39;] = hotStr;

             

        }

    //}

};

/* 城市HTML模板 */

/* 城市HTML模板 */

Vcity._template = [

    &#39;<p class="tip">热门城市(支持汉字/拼音)</p>&#39;,

    &#39;<ul>&#39;,

    &#39;<li class="on">热门城市</li>&#39;,

    &#39;<li>A</li>&#39;,

    &#39;<li>B</li>&#39;,

    &#39;<li>C</li>&#39;,

    &#39;<li>D</li>&#39;,

    &#39;<li>E</li>&#39;,

    &#39;<li>F</li>&#39;,

    &#39;<li>G</li>&#39;,

    &#39;<li>H</li>&#39;,

    &#39;<li>I</li>&#39;,

    &#39;<li>J</li>&#39;,

    &#39;<li>K</li>&#39;,

    &#39;<li>L</li>&#39;,

    &#39;<li>M</li>&#39;,

    &#39;<li>N</li>&#39;,

    &#39;<li>O</li>&#39;,

    &#39;<li>P</li>&#39;,

    &#39;<li>Q</li>&#39;,

    &#39;<li>R</li>&#39;,

    &#39;<li>S</li>&#39;,

    &#39;<li>T</li>&#39;,

    &#39;<li>U</li>&#39;,

    &#39;<li>V</li>&#39;,

    &#39;<li>W</li>&#39;,

    &#39;<li>x</li>&#39;,

    &#39;<li>y</li>&#39;,

    &#39;<li>Z</li>&#39;,

    &#39;</ul>&#39;

];

/* 城市HTML模板 */

/*Vcity._template = [

    &#39;<p class="tip">热门城市(支持汉字/拼音)</p>&#39;,

    &#39;<ul>&#39;,

    &#39;<li class="on">热门城市</li>&#39;,

    &#39;<li>ABCDEFGH</li>&#39;,

    &#39;<li>IJKLMNOP</li>&#39;,

    &#39;<li>QRSTUVWXYZ</li>&#39;,

    &#39;</ul>&#39;

];*/

/* *

 * 城市控件构造函数

 * @CitySelector

 * */

Vcity.CitySelector = function () {

    this.initialize.apply(this, arguments);

};

Vcity.CitySelector.prototype = {

    constructor:Vcity.CitySelector,

    /* 初始化 */

    initialize :function (options,istr,hotStr) {

        var input = options.input;

        this.input = Vcity._m.$(&#39;#&#39;+ input);

         

        this.inputEvent(istr,hotStr);

         

    },

    /* *

     * @createWarp

     * 创建城市BOX HTML 框架

     * */

    createWarp:function(cwstr,hotStr){

        var inputPos = Vcity._m.getPos(this.input);

        var p = this.rootDiv = document.createElement(&#39;p&#39;);

        var that = this;

        // 设置DIV阻止冒泡

        Vcity._m.on(this.rootDiv,&#39;click&#39;,function(event){

            Vcity._m.stopPropagation(event);

        });

        // 设置点击文档隐藏弹出的城市选择框

        Vcity._m.on(document, &#39;click&#39;, function (event) {

            event = Vcity._m.getEvent(event);

            var target = Vcity._m.getTarget(event);

            if(target == that.input) return false;

            //console.log(target.className);

            if (that.cityBox)Vcity._m.addClass(&#39;hide&#39;, that.cityBox);

            if (that.ul)Vcity._m.addClass(&#39;hide&#39;, that.ul);

            if(that.myIframe)Vcity._m.addClass(&#39;hide&#39;,that.myIframe);

        });

        p.className = &#39;citySelector&#39;;

        p.style.position = &#39;absolute&#39;;

        p.style.left = inputPos.left + &#39;px&#39;;

        p.style.top = inputPos.bottom + &#39;px&#39;;

        p.style.zIndex = 999999;

        // 判断是否IE6,如果是IE6需要添加iframe才能遮住SELECT框

        var isIe = (document.all) ? true : false;

        var isIE6 = this.isIE6 = isIe && !window.XMLHttpRequest;

        if(isIE6){

            var myIframe = this.myIframe =  document.createElement(&#39;iframe&#39;);

            myIframe.frameborder = &#39;0&#39;;

            myIframe.src = &#39;about:blank&#39;;

            myIframe.style.position = &#39;absolute&#39;;

            myIframe.style.zIndex = &#39;-1&#39;;

            this.rootDiv.appendChild(this.myIframe);

        }

        var childp = this.cityBox = document.createElement(&#39;p&#39;);

        childp.className = &#39;cityBox&#39;;

        childp.id = &#39;cityBox&#39;;

        childp.innerHTML = Vcity._template.join(&#39;&#39;);

        var hotCity = this.hotCity =  document.createElement(&#39;p&#39;);

        hotCity.className = &#39;hotCity&#39;;

        childp.appendChild(hotCity);

        p.appendChild(childp);

        this.createHotCity(cwstr,hotStr);

    },

    /* *

     * @createHotCity

     * TAB下面DIV:hot,a-h,i-p,q-z 分类HTML生成,DOM操作

     * {HOT:{hot:[]},ABCDEFGH:{a:[1,2,3],b:[1,2,3]},IJKLMNOP:{},QRSTUVWXYZ:{}}

     **/

    createHotCity:function(chstr,hotStr){

          adapterCity (chstr,hotStr);

        var op,odl,odt,odd,odda=[],str,key,ckey,sortKey,regEx = Vcity.regEx,

            oCity = Vcity.oCity;

        for(key in oCity){

            op = this[key] = document.createElement(&#39;p&#39;);

            // 先设置全部隐藏hide

            op.className = key + &#39; &#39; + &#39;cityTab hide&#39;;

            sortKey=[];

            for(ckey in oCity[key]){

                sortKey.push(ckey);

                // ckey按照ABCDEDG顺序排序

                sortKey.sort();

            }

            for(var j=0,k = sortKey.length;j<k;j++){

                odl = document.createElement(&#39;dl&#39;);

                odt = document.createElement(&#39;dt&#39;);

                odd = document.createElement(&#39;dd&#39;);

                odt.innerHTML = sortKey[j] == &#39;hot&#39;?&#39; &#39;:sortKey[j];

                odda = [];

                for(var i=0,n=oCity[key][sortKey[j]].length;i<n;i++){

                    str = &#39;<a href="javascript:">&#39; + oCity[key][sortKey[j]][i] + &#39;</a>&#39;;

                    odda.push(str);

                }

                odd.innerHTML = odda.join(&#39;&#39;);

                odl.appendChild(odt);

                odl.appendChild(odd);

                op.appendChild(odl);

            }

            // 移除热门城市的隐藏CSS

            Vcity._m.removeClass(&#39;hide&#39;,this.hot);

            this.hotCity.appendChild(op);

        }

        document.body.appendChild(this.rootDiv);

        /* IE6 */

        this.changeIframe();

        this.tabChange();

        this.linkEvent();

    },

    /* *

     *  tab按字母顺序切换

     *  @ tabChange

     * */

    tabChange:function(){

        var lis = Vcity._m.$(&#39;li&#39;,this.cityBox);

        var ps = Vcity._m.$(&#39;p&#39;,this.hotCity);

        var that = this;

        for(var i=0,n=lis.length;i<n;i++){

            lis[i].index = i;

            lis[i].onclick = function(){

                for(var j=0;j<n;j++){

                    Vcity._m.removeClass(&#39;on&#39;,lis[j]);

                    Vcity._m.addClass(&#39;hide&#39;,ps[j]);

                }

                Vcity._m.addClass(&#39;on&#39;,this);

                Vcity._m.removeClass(&#39;hide&#39;,ps[this.index]);

                /* IE6 改变TAB的时候 改变Iframe 大小*/

                that.changeIframe();

            };

        }

    },

    /* *

     * 城市LINK事件

     *  @linkEvent

     * */

    linkEvent:function(){

        var links = Vcity._m.$(&#39;a&#39;,this.hotCity);

        var that = this;

        for(var i=0,n=links.length;i<n;i++){

            links[i].onclick = function(){

                that.input.value = this.innerHTML;

                Vcity._m.addClass(&#39;hide&#39;,that.cityBox);

                /* 点击城市名的时候隐藏myIframe */

                Vcity._m.addClass(&#39;hide&#39;,that.myIframe);

            }

        }

    },

    /* *

     * INPUT城市输入框事件

     * @inputEvent

     * */

    inputEvent:function(iestr,hotStr){

        var that = this;

        Vcity._m.on(this.input,&#39;click&#39;,function(event){

            event = event || window.event;

            if(!that.cityBox){

                that.createWarp(iestr,hotStr);

            }else if(!!that.cityBox && Vcity._m.hasClass(&#39;hide&#39;,that.cityBox)){

                // slideul 不存在或者 slideul存在但是是隐藏的时候 两者不能共存

                if(!that.ul || (that.ul && Vcity._m.hasClass(&#39;hide&#39;,that.ul))){

                    Vcity._m.removeClass(&#39;hide&#39;,that.cityBox);

                    /* IE6 移除iframe 的hide 样式 */

                    //alert(&#39;click&#39;);

                    Vcity._m.removeClass(&#39;hide&#39;,that.myIframe);

                    that.changeIframe();

                }

            }

        });

        Vcity._m.on(this.input,&#39;focus&#39;,function(){

            that.input.select();

            if(that.input.value == &#39;城市名&#39;) that.input.value = &#39;&#39;;

        });

        Vcity._m.on(this.input,&#39;blur&#39;,function(){

            if(that.input.value == &#39;&#39;) that.input.value = &#39;城市名&#39;;

        });

        Vcity._m.on(this.input,&#39;keyup&#39;,function(event){

            event = event || window.event;

            var keycode = event.keyCode;

            Vcity._m.addClass(&#39;hide&#39;,that.cityBox);

            that.createUl(iestr);

            /* 移除iframe 的hide 样式 */

            Vcity._m.removeClass(&#39;hide&#39;,that.myIframe);

            // 下拉菜单显示的时候捕捉按键事件

            if(that.ul && !Vcity._m.hasClass(&#39;hide&#39;,that.ul) && !that.isEmpty){

                that.KeyboardEvent(event,keycode);

            }

        });

    },

    /* *

     * 生成下拉选择列表

     * @ createUl

     * */

    createUl:function (iestr) {

        if(!iestr){

            return;

        }

        //console.log(&#39;createUL&#39;);

        var str;

        var value = Vcity._m.trim(this.input.value);

        // 当value不等于空的时候执行

        if (value !== &#39;&#39;) {

            var reg = new RegExp("^" + value + "|\\|" + value, &#39;gi&#39;);

            var searchResult = [];

            /* *

             *【修改5】

             * 这里原本是遍历Vcity.allCity,现在改用citylist

             * zcx 2016-5-17

             */

             

            for (var i = 0; i < iestr.length; i++) {

                if (reg.test(iestr[i])) {

                    var match = Vcity.regEx.exec(iestr[i]);

                    if (searchResult.length !== 0) {

                        str = &#39;<li><b class="cityname">&#39; + match[1] + &#39;</b><b class="cityspell">&#39; + match[2] + &#39;</b></li>&#39;;

                    } else {

                        str = &#39;<li class="on"><b class="cityname">&#39; + match[1] + &#39;</b><b class="cityspell">&#39; + match[2] + &#39;</b></li>&#39;;

                    }

                    searchResult.push(str);

                }

            }

            this.isEmpty = false;

            // 如果搜索数据为空

            if (searchResult.length == 0) {

                this.isEmpty = true;

                str = &#39;<li class="empty">对不起,没有找到数据 "<em>&#39; + value + &#39;</em>"</li>&#39;;

                searchResult.push(str);

            }

            // 如果slideul不存在则添加ul

            if (!this.ul) {

                var ul = this.ul = document.createElement(&#39;ul&#39;);

                ul.className = &#39;cityslide&#39;;

                this.rootDiv && this.rootDiv.appendChild(ul);

                // 记录按键次数,方向键

                this.count = 0;

            } else if (this.ul && Vcity._m.hasClass(&#39;hide&#39;, this.ul)) {

                this.count = 0;

                Vcity._m.removeClass(&#39;hide&#39;, this.ul);

            }

            this.ul.innerHTML = searchResult.join(&#39;&#39;);

            /* IE6 */

            this.changeIframe();

            // 绑定Li事件

            this.liEvent();

        }else{

            Vcity._m.addClass(&#39;hide&#39;,this.ul);

            Vcity._m.removeClass(&#39;hide&#39;,this.cityBox);

            Vcity._m.removeClass(&#39;hide&#39;,this.myIframe);

            this.changeIframe();

        }

    },

    /* IE6的改变遮罩SELECT 的 IFRAME尺寸大小 */

    changeIframe:function(){

        if(!this.isIE6)return;

        this.myIframe.style.width = this.rootDiv.offsetWidth + &#39;px&#39;;

        this.myIframe.style.height = this.rootDiv.offsetHeight + &#39;px&#39;;

    },

    /* *

     * 特定键盘事件,上、下、Enter键

     * @ KeyboardEvent

     * */

    KeyboardEvent:function(event,keycode){

        var lis = Vcity._m.$(&#39;li&#39;,this.ul);

        var len = lis.length;

        switch(keycode){

            case 40: //向下箭头↓

                this.count++;

                if(this.count > len-1) this.count = 0;

                for(var i=0;i<len;i++){

                    Vcity._m.removeClass(&#39;on&#39;,lis[i]);

                }

                Vcity._m.addClass(&#39;on&#39;,lis[this.count]);

                break;

            case 38: //向上箭头↑

                this.count--;

                if(this.count<0) this.count = len-1;

                for(i=0;i<len;i++){

                    Vcity._m.removeClass(&#39;on&#39;,lis[i]);

                }

                Vcity._m.addClass(&#39;on&#39;,lis[this.count]);

                break;

            case 13: // enter键

                this.input.value = Vcity.regExChiese.exec(lis[this.count].innerHTML)[0];

                Vcity._m.addClass(&#39;hide&#39;,this.ul);

                Vcity._m.addClass(&#39;hide&#39;,this.ul);

                /* IE6 */

                Vcity._m.addClass(&#39;hide&#39;,this.myIframe);

                break;

            default:

                break;

        }

    },

    /* *

     * 下拉列表的li事件

     * @ liEvent

     * */

    liEvent:function(){

        var that = this;

        var lis = Vcity._m.$(&#39;li&#39;,this.ul);

        for(var i = 0,n = lis.length;i < n;i++){

            Vcity._m.on(lis[i],&#39;click&#39;,function(event){

                event = Vcity._m.getEvent(event);

                var target = Vcity._m.getTarget(event);

                that.input.value = Vcity.regExChiese.exec(target.innerHTML)[0];

                Vcity._m.addClass(&#39;hide&#39;,that.ul);

                /* IE6 下拉菜单点击事件 */

                Vcity._m.addClass(&#39;hide&#39;,that.myIframe);

            });

            Vcity._m.on(lis[i],&#39;mouseover&#39;,function(event){

                event = Vcity._m.getEvent(event);

                var target = Vcity._m.getTarget(event);

                Vcity._m.addClass(&#39;on&#39;,target);

            });

            Vcity._m.on(lis[i],&#39;mouseout&#39;,function(event){

                event = Vcity._m.getEvent(event);

                var target = Vcity._m.getTarget(event);

                Vcity._m.removeClass(&#39;on&#39;,target);

            })

        }

    }

}

Copy after login

更多Multi-city selector implementation code请关注PHP中文网(www.php.cn)其它文章!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)