I used to think that script can be placed anywhere in HTML. I corrected my misconception when I made a request today - the location of script is not random. This article mainly talks about the placement of script in HTML. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Hope it helps everyone.
First of all, I want to implement a select tag with two options: yes and no. However, during initialization, the select tag is required to select a null value by default, so I added a method to let it change the null value when clicking. Delete:
XML/HTML Code Copy content to clipboard
nbsp;html> <script></script> <script> $('#checkcash').click(function () { if ($('#checkcash').val() == '0') { $("#checkcash option[value='0']").remove(); } }); $("#alert").click(function(){ alert("1123"); }) </script> 是否已提现 <select> <option></option> <option>是</option> <option>否</option> </select> <input>
But this cannot achieve the desired effect. At first I thought it was a jquery syntax error and I kept checking and correcting it online, but it didn’t work. Then I suddenly thought about whether I should I put the script in the back and tried it, and it turned out to be ok. Then I suddenly realized that it was not like this.
Later I checked the reason because the html file is executed in a top-down manner, but the order of the imported css and javascript is different. When the css is introduced and loaded, the program is still executed downwards, and When the <script> script is executed, the thread is interrupted, and the program continues execution only after the execution of the script script is completed. Therefore, the script is generally placed after the body to avoid delayed blocking due to long execution of the script. To achieve the effects of some pages, some js scripts need to be dynamically loaded in advance, so these scripts should be placed before <body>. Secondly, the js that needs to access the dom element cannot be placed before the body, because the dom has not been generated at this time, so the js that accesses the dom element before the body will be wrong or invalid. It is because of this that I added methods to the dom before it was generated, which led to this. </script>
I really should study more. I think it’s so, but I don’t delve into it. Keep working hard in the future!
ps: In fact, there is another way, which is to use jquery's initialization page method. It is also possible to add the click event added to the label above to $(function(){}). The principle is the same It’s the same as above. This method is executed after the page is loaded, so it can be used no matter where it is placed!
Related recommendations:
Detailed explanation of immediate execution function examples in JavaScript
A brief introduction to JavaScript execution sequence
Detailed introduction to JavaScript execution order_Basic knowledge
The above is the detailed content of Analysis on the placement position of script in html. For more information, please follow other related articles on the PHP Chinese website!

本篇文章带大家了解一下HTML(超文本标记语言),介绍一下HTML的本质,HTML文档的结构、HTML文档的基本标签和图像标签、列表、表格标签、媒体元素、表单,希望对大家有所帮助!

总结了一些web前端面试(笔试)题分享给大家,本篇文章就先给大家分享HTML部分的笔试题(附答案),大家可以自己做做,看看能答对几个!

script是指剧本或脚本的意思。在电影、电视、戏剧等艺术形式中,script用于描述角色的对话、动作和场景,以及故事的发展和结构。script的编写需要一定的技巧和经验,而且应该生动、有力,能够吸引观众的注意力,并传达出故事的情感和主题。script在电影和电视行业中尤为重要,是创作的基础,决定了电影的故事情节、角色发展和对话内容。script是艺术家们创作和表达的重要工具。

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

计算机科学领域中,"script"通常表示一种脚本语言或脚本文件,脚本语言是一种解释性编程语言,通常用于自动化、批处理和快速原型开发等任务。

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

scripterror的解决办法有检查语法、文件路径、检查网络连接、浏览器兼容性、使用try-catch语句、使用开发者工具进行调试、更新浏览器和JavaScript库或寻求专业帮助等。详细介绍:1、检查语法错误:Script Error可能是由于JavaScript代码中存在语法错误导致的,使用开发者工具来检查代码并修复语法错误,确保代码中的括号、引号、分号等都是正确的等等。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
