Table of Contents
Reply to discussion (solution)
Home Web Front-end HTML Tutorial Regarding the $('body').append() piece of html code, writing it on the page can trigger an event, but writing it in a js file will not_html/css_WEB-ITnose

Regarding the $('body').append() piece of html code, writing it on the page can trigger an event, but writing it in a js file will not_html/css_WEB-ITnose

Jun 24, 2016 am 11:58 AM
append body event

$('body').prepend('<div id='nav' class='navbar'><div class='navbar-content'><a href='/' class='element'><span class='icon-grid-view'></span> 后台管理 <sup>1.0 Beta</sup></a> <span class='element-divider'></span> <a class='pull-menu' href='#'></a><div class='no-tablet-portrait'><div class='element place-right'><a class='dropdown-toggle' href='#'><span id='spanUser'>刁静?2</span><span class='icon-user-2'></span></a><ul class='dropdown-menu place-right' data-role='dropdown'><li><a href='#'>密码修改</a></li></ul></div><span class='element-divider place-right'></span></div></div></div>');
Copy after login


For the same piece of code, if I write this js directly on the page, it is absolutely fine. But if I write it in a js file (this js file was introduced later than the UI framework js), this code only renders the style, but the event has no effect at all.

For example, in the picture, after clicking on the user, a drop-down menu will pop up.
If js is written in the page, click "↓" and the "Change Password" menu will pop up
But if js is written in a separate js file, and then import this js file, click "↓" and nothing will happen reflect.

What could be causing this?


Reply to discussion (solution)

This depends on the loading order of js files

This js file was introduced later than the UI framework js)



In this way, you will miss the initialization event in the framework code


this js files were introduced later than the UI framework js)



In this way, you miss the initialization event in the framework code



I don’t understand it at all

    <link href="css/UI.css" rel="stylesheet" />    <script src="js/UI.js"></script><body></body><script type="text/javascript">$("body").append()</script>
Copy after login


Now you can write it like this, and then I will change the <script> directly to src="XX.js" here. In this way, the changed loading order and the order of directly writing the code should be the same. Same?

up `````

You can try to quote it before the body tag is closed, not necessarily in the head. Just like JS can be written anywhere.

You can try to quote it before the body tag is closed, not necessarily in the head. Just like JS can be written anywhere.



But I didn’t quote it in the head. .
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 Article Tags

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)

Event ID 4660: Object deleted [Fix] Event ID 4660: Object deleted [Fix] Jul 03, 2023 am 08:13 AM

Event ID 4660: Object deleted [Fix]

Get upcoming calendar events on your iPhone lock screen Get upcoming calendar events on your iPhone lock screen Dec 01, 2023 pm 02:21 PM

Get upcoming calendar events on your iPhone lock screen

In JavaScript, what is the purpose of the 'oninput' event? In JavaScript, what is the purpose of the 'oninput' event? Aug 26, 2023 pm 03:17 PM

In JavaScript, what is the purpose of the 'oninput' event?

How to implement calendar functions and event reminders in PHP projects? How to implement calendar functions and event reminders in PHP projects? Nov 02, 2023 pm 12:48 PM

How to implement calendar functions and event reminders in PHP projects?

How to implement change event binding of select elements in jQuery How to implement change event binding of select elements in jQuery Feb 23, 2024 pm 01:12 PM

How to implement change event binding of select elements in jQuery

Detailed explanation of input box binding events in Vue documents Detailed explanation of input box binding events in Vue documents Jun 21, 2023 am 08:12 AM

Detailed explanation of input box binding events in Vue documents

What are the commonly used events in jquery What are the commonly used events in jquery Jan 03, 2023 pm 06:13 PM

What are the commonly used events in jquery

Use java's StringBuilder.append() function to splice strings Use java's StringBuilder.append() function to splice strings Jul 26, 2023 am 09:18 AM

Use java's StringBuilder.append() function to splice strings

See all articles