Home Web Front-end JS Tutorial How to implement multi-level drop-down menu jDropMenu with jQuery_jquery

How to implement multi-level drop-down menu jDropMenu with jQuery_jquery

May 16, 2016 pm 03:41 PM
jquery Drop-down menu

The example in this article describes how jQuery implements the multi-level drop-down menu jDropMenu. Share it with everyone for your reference. The details are as follows:

The jQuery multi-level drop-down menu navigation introduced here - multi-level drop-down menu, the English name is DropDown Menu. Add support for corresponding handling events mouseover, mouseout and tab operations through each() traversal. This effect is one of the most common effects we usually use, and it is also a very practical JavaScript special effect. For example, the main navigation of my BlueNight theme uses this effect of multi-level drop-down menus.

The screenshot of the running effect is as follows:

The online demo address is as follows:

http://demo.jb51.net/js/2015/jquery-nlevel-down-show-jdropmenu-codes/

The specific code is as follows:

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

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>jDropMenu - jQuery 多级下拉菜单插件</title>

<style type="text/css">

#top-navigation {

 margin: 10px auto 600px;

 width:960px;

 height:36px;

}

.menu {

 margin: 0;

}

.menu-item {

 position: relative;

 z-index: 2;

 float: left;

 margin-left: 3px;

 font-size: 14px;

 width: 115px;

 line-height: 36px;

 list-style-type: none;

 text-align: center;

 display: inline;

}

.menu .has-sub-menu {

 border-top: 3px solid #369;

 line-height: 33px;

}

.sub-menu .has-sub-menu {

 border-top: none;

 line-height: 36px;

 border-right: 3px solid #369;

 width: 112px;

}

.menu-item a:link, .menu-item a:visited, .menu-item a:hover {

 font-weight: bold;

 display: block;

 width: 100%;

 background-color: #3B3939;

 color: #fff;

 overflow: hidden;

}

.menu-item a:hover {

 color: #009FBC;

 background-color: #FFF;

 text-decoration: none;

}

.menu-item .sub-menu {

 display: none;

 position: absolute;

 z-index: 3;

 top: 33px;

 left: 0;

 margin: 0;

 box-shadow: 0 3px 10px #333;

 width: 115px;

}

.sub-menu li {

 margin-left: 0;

}

.sub-menu .sub-menu {

 top: 0;

 left: 115px;

 z-index: 4;

}

</style>

</head>

<body>

<div class="header">

 <h1>jDropMenu - jQuery 多级下拉菜单插件</h1>

</div>

<div id="top-navigation">

<ul class="menu" id="menu-top-navigation">

 <li class="menu-item">

 <a href="#">Home</a>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">Frontend</a>

 <ul class="sub-menu">

 <li class="menu-item">

 <a href="#">XHTML</a>

 </li>

 <li class="menu-item">

 <a href="#">CSS</a>

 </li>

 <li class="menu-item">

 <a href="#">HTML5</a>

 </li>

 <li class="menu-item">

 <a href="#">CSS 3</a>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">JavaScript</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item has-sub-menu">

  <a href="#">YUI</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item">

  <a href="#">YUI</a>

  </li>

 </ul>

  </li>

 </ul>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">DOM</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item">

  <a href="#">YUI</a>

  </li>

 </ul>

 </li>

 <li class="menu-item">

 <a href="#">Ajax</a>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">JSON</a>

 <ul class="sub-menu">

 <li class="menu-item">

 <a href="#">XHTML</a>

 </li>

 <li class="menu-item">

 <a href="#">CSS</a>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">HTML5</a>

 <ul class="sub-menu">

 <li class="menu-item">

 <a href="#">XHTML</a>

 </li>

 <li class="menu-item">

 <a href="#">CSS</a>

 </li>

 <li class="menu-item">

 <a href="#">HTML5</a>

 </li>

 <li class="menu-item">

 <a href="#">CSS 3</a>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">JavaScript</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item has-sub-menu">

  <a href="#">YUI</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item">

  <a href="#">YUI</a>

  </li>

 </ul>

  </li>

 </ul>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">DOM</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item">

  <a href="#">YUI</a>

  </li>

 </ul>

 </li>

 <li class="menu-item">

 <a href="#">Ajax</a>

 </li>

 <li class="menu-item">

 <a href="#">JSON</a>

 </li>

 </ul>

 </li>

 <li class="menu-item">

 <a href="#">CSS 3</a>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">JavaScript</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item has-sub-menu">

  <a href="#">YUI</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item">

  <a href="#">YUI</a>

  </li>

 </ul>

  </li>

 </ul>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">DOM</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item">

  <a href="#">YUI</a>

  </li>

 </ul>

 </li>

 <li class="menu-item">

 <a href="#">Ajax</a>

 </li>

 <li class="menu-item">

 <a href="#">JSON</a>

 </li>

 </ul>

 </li>

 </ul>

 </li>

 <li class="menu-item">

 <a href="#">PHP</a>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">WordPress</a>

 <ul class="sub-menu">

 <li class="menu-item">

 <a href="#">WP Themes</a>

 </li>

 <li class="menu-item">

 <a href="#">WP Plugins</a>

 </li>

 </ul>

 </li>

 <li class="menu-item">

 <a href="#">SEO</a>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">Video</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item has-sub-menu">

  <a href="#">YUI</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item has-sub-menu">

  <a href="#">YUI</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item has-sub-menu">

  <a href="#">YUI</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item">

  <a href="#">YUI</a>

  </li>

 </ul>

  </li>

 </ul>

  </li>

 </ul>

  </li>

 </ul>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">Downloads</a>

 <ul class="sub-menu">

 <li class="menu-item">

 <a href="#">XHTML</a>

 </li>

 <li class="menu-item">

 <a href="#">CSS</a>

 </li>

 <li class="menu-item">

 <a href="#">HTML5</a>

 </li>

 <li class="menu-item">

 <a href="#">CSS 3</a>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">JavaScript</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item has-sub-menu">

  <a href="#">YUI</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item">

  <a href="#">YUI</a>

  </li>

 </ul>

  </li>

 </ul>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">DOM</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item">

  <a href="#">YUI</a>

  </li>

 </ul>

 </li>

 <li class="menu-item">

 <a href="#">Ajax</a>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">JSON</a>

 <ul class="sub-menu">

 <li class="menu-item">

 <a href="#">XHTML</a>

 </li>

 <li class="menu-item">

 <a href="#">CSS</a>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">HTML5</a>

 <ul class="sub-menu">

 <li class="menu-item">

 <a href="#">XHTML</a>

 </li>

 <li class="menu-item">

 <a href="#">CSS</a>

 </li>

 <li class="menu-item">

 <a href="#">HTML5</a>

 </li>

 <li class="menu-item">

 <a href="#">CSS 3</a>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">JavaScript</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item has-sub-menu">

  <a href="#">YUI</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item">

  <a href="#">YUI</a>

  </li>

 </ul>

  </li>

 </ul>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">DOM</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item">

  <a href="#">YUI</a>

  </li>

 </ul>

 </li>

 <li class="menu-item">

 <a href="#">Ajax</a>

 </li>

 <li class="menu-item">

 <a href="#">JSON</a>

 </li>

 </ul>

 </li>

 <li class="menu-item">

 <a href="#">CSS 3</a>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">JavaScript</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item has-sub-menu">

  <a href="#">YUI</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item">

  <a href="#">YUI</a>

  </li>

 </ul>

  </li>

 </ul>

 </li>

 <li class="menu-item has-sub-menu">

 <a href="#">DOM</a>

 <ul class="sub-menu">

  <li class="menu-item">

  <a href="#">jQuery</a>

  </li>

  <li class="menu-item">

  <a href="#">YUI</a>

  </li>

 </ul>

 </li>

 <li class="menu-item">

 <a href="#">Ajax</a>

 </li>

 <li class="menu-item">

 <a href="#">JSON</a>

 </li>

 </ul>

 </li>

 </ul>

 </li>

 <li class="menu-item">

 <a href="#">About Us</a>

 </li>

</ul>

</div>

 <script type="text/javascript" src="jquery-1.6.2.min.js"></script>

 <script type="text/javascript">

 (function($){

 $.fn.extend({

 dropMenu: function(menuItem, subMenuItem){

 var root = $(this), // 首先找到菜单(的根节点)

 CLS_HAS_MENU = 'has-sub-menu',

 isIE = $.browser.msie, // 是否为 IE 浏览器

 version = $.browser.version; // 浏览器的版本

 // 没有找到菜单则退出

 // root.find(':first') 都是得到 document.getElementById('top-navigation')

 if (!root[0]) {

 return false;

 }

 // 默认的菜单标签为 li 标签(选择器)

 if (!menuItem) {

 menuItem = 'li';

 }

 // 默认的子菜单标签为 ul 标签(选择器)

 if (!subMenuItem) {

 subMenuItem = 'ul';

 }

 // $(root).find(menuItem) 找到导航菜单下所有的 li 节点

 // 通过 each() 遍历添加相应的处理事件 mouseover,mouseout 和 tab 操作的支持

 $(root).find(menuItem).each(function(i, li){

 var curMenu = $(li),

 // 找到 li 下的第一个 a 标签,添加 tab 键的支持时需要用到的

 curLink = curMenu.is('a') &#63; curMenu : $(curMenu.find('a:first')),

 // 找到当前 li 标签下的子菜单

 subMenus = $(subMenuItem, curMenu),

 // 判断是否有子菜单节点

 hasMenu = subMenus.length >= 1,

 // 当前 li 标签下的子菜单

 curSubMenu = null,

 // 当前子菜单的最后一项(a 标签)

 curSubMenuLastItem = null,

 // 显示子菜单

 show = function(){

 // show sub menu 发现 IE6 中使用 show() 方无法显示二级以下的子菜单

 // 所以很无赖的 hack 了一下

 if (!isIE || (isIE && version > 6)) {

  curSubMenu.show(200);

 }

 else {

  curSubMenu.css('display', 'block');

 }

 },

 // 隐藏子菜单

 hide = function(){

 // hide sub menu

 if (!isIE || (isIE && version > 6)) {

  curSubMenu.hide(150);

 }

 else {

  curSubMenu.css('display', 'none');

 }

 };

 // 只在有子菜单的时候才做相应的处理

 if (hasMenu) {

 // 无赖 WordPress 的输出没有 .has-sub-menu

 // 只要自己手动加上了

 curMenu.addClass(CLS_HAS_MENU);

 // 找到当前 li 对应的子菜单,而不是把更次级的子菜单都找到

 // 不习惯用 subMenus.get(0)

 curSubMenu = $(subMenus[0]);

 // 当前子菜单的最后一项(a 标签)

  curSubMenuLastItem = curSubMenu.find('a:last');

 // mouse event

 curMenu.hover(show, hide);

 // key(tab key) event

 // 获得焦点是在当前 li 下的第一个A标签上处理

 curLink.focus(show);

 // 失去焦点则需要是tab让子菜单的最后一个菜单项都走过了,才关闭子菜单

 curSubMenuLastItem.blur(hide);

 }

 });

 }

 });

 $('#top-navigation').dropMenu();

})(jQuery);

 </script>

 <script type="text/javascript">

 (function(){

 var _gaq = _gaq || [];

 _gaq.push(['_setAccount', 'UA-4473199-1']);

 _gaq.push(['_trackPageview']);

 (function(){

  var ga = document.createElement('script');

  ga.type = 'text/javascript';

  ga.async = true;

  ga.src = ('https:' == document.location.protocol &#63; 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

  var s = document.getElementsByTagName('script')[0];

  s.parentNode.insertBefore(ga, s);

 })();

 })();

 </script>

</body>

</html>

Copy after login

I hope this article will be helpful to everyone’s jquery programming design.

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 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)

How to make drop-down menu in WPS table How to make drop-down menu in WPS table Mar 21, 2024 pm 01:31 PM

How to make the WPS table drop-down menu: After selecting the cell where you want to set the drop-down menu, click &quot;Data&quot;, &quot;Validity&quot; in sequence, and then make the corresponding settings in the pop-up dialog box to pull down our menu. As a powerful office software, WPS has the ability to edit documents, statistical data tables, etc., which provides a lot of convenience for many people who need to deal with text, data, etc. In order to skillfully use WPS software to provide us with a lot of convenience, we need to be able to master various very basic operations of WPS software. In this article, the editor will share with you how to use WPS software. Perform drop-down menu operations in the WPS table that appears. After opening the WPS form, first select the

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to set up excel drop-down menu to automatically associate data? Excel drop-down menu automatically associates data settings How to set up excel drop-down menu to automatically associate data? Excel drop-down menu automatically associates data settings Mar 13, 2024 pm 03:04 PM

How to set up Excel drop-down menu to automatically associate data? When we use excel, we use drop-down menus to quickly operate our data. However, many users also ask how to set up the drop-down menu to automatically associate data? Let this site introduce to users in detail how to set up automatic data association for excel drop-down menus. How to set up excel drop-down menu to automatically associate data 1. Open the Excel table. 2. Enter a piece of related data in a blank cell. 3. Then select the cells where you want to add a drop-down list. 4. Click [Data]-[Data Verification] on the menu bar. 5. Select [Sequence] for verification conditions. 6. Click the button pointed by the arrow in the picture and select

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

See all articles