、html()方法:如果想更改或者是设置 HTML 的内容,我们可以使用 html()方法,首先我们先使用这个方法获取元素里面的内容 var html=$("p").html()。如果需要设置某元素的 HTML 代码,那么我们就可以使用此方法加上一个参数。此方法只能应用于 XHTML 中,不能用于 xml。
2、text()方法,去设置某个元素中的文本内容,代码是 var text=$("p").text();如果想设置文本同样需要给它传一个参数。
3、val()方法,可以用来设置和获取元素的值,它不仅仅可以设置元素,同时也能获取元素,另外,它能是下拉列表框,多选框,和单选框相应的选项被选中,在表单操作中会经常用到。
本文主要记录常用的html标签使用说明,用起来的时候偶尔查查。
标签 | 英文全拼 | 作用 | 特点 |
?<html></html>?? | html | 网页的根标签 | 所有的标签都要写在这一对根标签里面 |
??<head></head>?? | head | 网页的头标签 | 包括完档的属性和信息 |
??<body></body>?? | body | 网页的主题 | 包含文档的所有内容 |
??<div></div>?? | division | 定义一个区域 | 浏览器通常会在??<div>??前后放置一个换行符 |
??<!-- 注释 -->?? | - | 注释 | 单标签 |
??<br>或<br/>?? | break | 换行 | 单标签,不会在其前后创建空白行 |
??<hr>或<hr/>?? | horizontal rule | 添加水平线 | 单标签 |
??<img src="">?? | image | 添加图片 | 单标签 |
??<embed src="">?? | embed | 嵌入外部应用 | 单标签 |
??<meta>?? | meta | 提供有关页面的元信息 | 单标签,??<meta>???标签通常位于??<head>??区域内 |
??<link>?? | link | 定义文档与外部资源的关系 | 单标签,??<link>???标签只能存在于??<head>??区域内,不过它可出现任何次数。 |
??<p></p>?? | paragraph | 定义段落 | 自动在其前后创建空白行 |
??<h1> to <h6>?? | Header 1 to Header 6 | 定义标题 | h1在一个页面里只能出现一次 |
??<strong></strong>?? | strong | 文本加粗 | 加粗标记该文本 |
??<b></b>?? | bold | 文本加粗 | 加粗显示文本,不推荐使用 |
??<em></em>?? | emphasize | 文本倾斜 | 倾斜标记文本 |
??<i></i>?? | italic | 文本倾斜 | 倾斜显示文本,不推荐使用 |
??<del></del>?? | delete | 文本添加删除线 | - |
??<s></s>?? | strike | 文本添加删除线 | 不推荐使用 |
??<ins></ins>?? | insert | 文本添加下划线 | - |
??<u></u>?? | underline | 文本添加下划线 | 不推荐使用 |
??<a href="">填写内容</a>?? | anchor | 添加超链接 | 最好使用CSS来改变链接的样式 |
??<ul></ul>?? | unordered list | 定义无序列表 | 通常与??<li>??标签一起使用 |
??<ol></ol>?? | ordered list | 定义有序列表 | 通常与??<li>??标签一起使用 |
??<li></li>?? | list item | 创建列表项 | 可与各种列表定义标签一起使用 |
??<dl></dl>?? | definition list | 定义描述列表 | 通常与??<dt>???和??<dd>??一起使用 |
??<dt></dt>?? | definition term | 定义条目 | 定义描述列表的项目 |
??<dd></dd>?? | definition description | 定义描述 | 对描述列表中的项目进行描述 |
??<table></table>?? | table | 定义HTML表格 | 尽可能通过样式改变表格外观 |
??<tr></tr>?? | table row | 定义表格的行 | 一个??<tr>???标签包含一个或多个??<th>???或??<td>??标签 |
??<th></th>?? | table headline | 定义表格每一列的标题 | 该标签的文本通常呈现为粗体且居中 |
??<td></td>?? | table data | 定义表格中的单元格数据 | 该标签的文本呈现为普通且左对齐 |
??<caption>表格标题</caption>?? | caption | 定义整个表格的标题 | ??<caption>???标签必须直接放在??<table>??标签后 |
??<input type="">?? | input | 定义输入控件 | 输入字段可通过多种方式改变,取决于type属性 |
??select?? | select | 定义下拉列表 | ??<select>???中的??<option>??标签定义了列表中的可用选项 |
??<option></option>?? | option | 定义下拉列表中的可用项 | ??<option>??标签不可自由定义宽高 |
??<optgroup></optgroup>?? | options group | 定义选项组 | ??<optgroup>??标签用于把相关的选项组合在一起 |
??<textarea></textarea>?? | textarea | 定义多行的文本输入控件 | 文本的默认字体是等宽字体 |
??<form></form>?? | form | 定义表单 | ??<form>??可以包含多个元素 |
??<fieldset></fieldset>?? | field set | 定义围绕表单中元素的边框 | ??<legend>???为??<fieldset>??定义标题 |
??<legend></legend>?? | legend | 为??<fieldset>??定义标题 | ??<legend>??通过css设定样式 |
??<progress></progress>?? | progress | 定义运行中的任务进度 | ??<progress>???是HTML5中的新标签,??<progress>??标签不适合用来表示度量衡 |
??<meter></meter>?? | meter | 度量衡 | ??<meter>???是HTML5的新标签,??<meter>??标签不适合用来表示进度条 |
??<audio></audio>?? | audio | 添加音频 | ??<audio>??标签是HTML5的新标签 |
??<video></video>?? | video | 添加视频 | ??<video>??标签是HTML5的新标签 |
??<source>?? | source | 定义媒介资源 | ??<source>??标签是HTML5中的新标签 |
普通用法
锚点链接
锚点链接通过点击超链接,自动跳转到我们设置锚点的位置,类似于word的目录导航。建立锚点的元素必须要有id或name属性,最好两个都有。这里只跳转本页面元素,其他页面跳转自行搜索。
具体做法如下:
示例如下。为了显示效果,通过使用lorem自动生成随机文本(具体使用方法搜索,一般直接输入就行),lorem*50表示重复lorem15次。
<a href="#id2">a</a>
<p id="id1">
(lorem*15)
</p>
(lorem*15)
<p id="id2">
(lorem*15)
</p>
超链接全局设置
在页面head中写入代码可以设置超链接的全局跳转设置
<head>
<!-- 让页面所有的超链接新页面打开 -->
<base target="_blank">
</head>
charset编码
name
网页自动跳转
无序列表
无序列表使用粗体圆点进行标记。简单示例如下。
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
...
</ul>
有序列表
有序列表使用数字进行标记,我们可以通过整数值start指定列表编号的起始值。简单示例如下。
<ol start="2">
<li>a</li>
<li>b</li>
<li>c</li>
...
</ol>
描述列表
通过描述列表自定义列表,列表项内部可以使用段落、换行符、图片、链接以及其他列表等等。简单示例如下。
<dl>
<dt>A</dt> <!-- 小标题 -->
<dd>A1</dd> <!-- 解释标题 -->
<dd>A2</dd> <!-- 解释标题 -->
<dt>B</dt> <!-- 小标题 -->
<dd>B1</dd> <!-- 解释标题 -->
<dd>B2</dd> <!-- 解释标题 -->
</dl>
基础表格
简单示例如下。
<table width="300px" height="100px" border="2" cellspacing="5px" cellpadding="0">
<caption>表格标题</caption> <!-- 定义表格标题 -->
<tr>
<!-- 定义表格的行 -->
<td>A1</td> <!-- 定义表格该行第一列中的数据 -->
<td>B1</td>
<td>C1</td>
</tr>
<tr>
<!-- 定义表格的行 -->
<th>A</th> <!-- 定义表格每一列的标题 -->
<th>B</th>
<th>C</th>
</tr>
<tr>
<td>A2</td>
<td>B2</td>
<td>C2</td>
</tr>
</table>
单元格合并
简单示例如下。
<table border="2" cellspacing="1px" width="400px" height="100px">
<caption><strong>表格标题</strong></caption> <!-- 定义表格标题 -->
<tr height="100">
<!-- 定义表格的行 -->
<td colspan="2">A1</td> <!-- 定义该行可以横跨两列 -->
<td>B1</td>
</tr>
<tr height="100">
<td>A2</td>
<td>B2</td>
<td rowspan="2">C</td> <!-- 定义该行可以横跨两行 -->
</tr>
<tr height="100">
<td>A3</td>
<td>B3</td>
</tr>
</table>
对于??<input>??不同的type属性值,输入字段拥有很多种形式。输入字段可以是文本字段、复选框、掩码后的文本控件、单选按钮、按钮等等。
文本输入框
简单示例如下。
<input type="text" name="username" maxlength="6" readonly="readonly" disabled="disabled" value="用户名">
密码输入框
简单示例如下。
<input type="password" name="pwd" maxlength="6" readonly="readonly" disabled="disabled" value="密码">
单选框
示例一,两个单选框都可以被选中
<div>
<input type="radio" name="man" checked="checked">男
</div>
<div>
<input type="radio" name="woman">女
</div>
示例二,两个单选框只能有一个被选中
<div>
<input type="radio" name="gender" checked="checked">男
</div>
<div>
<input type="radio" name="gender">女
</div>
下拉列表
??<select>??
??<optgroup>??
??<option>??
示例一,单选下拉列表
<select>
<option value="a">a</option>
<option value="b">b</option>
<option value="c" selected='selected'>c</option> <!-- 默认选中 -->
</select>
示例二,带组合的单选下拉列表
<select>
<optgroup label="A">
<option value="a1">a1</option>
<option value="a2" selected='selected'>a2</option>
</optgroup>
<optgroup label="B">
<option value="b1">b1</option>
<option value="b2">b2</option>
</optgroup>
</select>
示例三,带组合的多选下拉列表
<select multiple=”multiple”>
<optgroup label="A">
<option value="a1">a1</option>
<option value="a2" selected='selected'>a2</option>
</optgroup>
<optgroup label="B">
<option value="b1" selected='selected'>b1</option>
<option value="b2">b2</option>
</optgroup>
</select>
多选框
简单示例如下。
<input type="checkbox" checked="checked">A
<input type="checkbox">B
文本框
简单示例如下。
<textarea cols="5" rows="2" placeholder="text"></textarea>
文本上传控件
简单示例如下。
<input type="file" accept="image/gif, image/jpeg"/>
其他类型按钮
<input type="submit">文件提交按钮
<input type="button" value="">普通按钮
<input type="image" src="">图片按钮
<input type="reset">重置按钮
<input type="url">网址控件
<input type="date">日期控件
<input type="time">时间控件
<!--email提供了邮箱的完整验证,必须包含@和后缀,如果不满足验证,会阻止表单提交-->
<input type="email">邮件控件
<input type="number" step="3">数字控件
<input type="range" step="100">滑块控件
<input type="color">颜色控件
表单
示例一,普通表单
<form>
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="提交">
</form>
示例二,带分组信息表单
<form>
<fieldset>
<legend>Personalia:</legend>
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="提交">
</fieldset>
First nameA: <input type="text" name="fname"><br>
Last nameB: <input type="text" name="lname"><br>
</form>
??<progress>???与??<meter>??主要区别和用法见??HTML5 progress和meter控件??
??<progress>??
简单示例如下。
<progress value="60" max="100"></progress>
??<meter>??
简单示例如下
<meter min="0" low="40" high="90" max="100" value="91"></meter>
<meter min="0" low="40" high="90" max="100" value="90"></meter>
??<audio>??
简单示例如下
<audio src="demo.mp3" controls autoplay></audio>
??<video>??
简单示例如下
<video src="demo.mp4" controls autoplay height="500px" poster="0.jpg"></video>
??<source>??
简单示例如下
<video controls>
<source src="demo.mp3" type="audio/mp3">
<source src="demo.mp4" type="video/mp4">
您的浏览器不支持video元素。
</video>
??HTML特殊字符编码对照表??
当用户进行鼠标框选选择了页面上的内容时,把选择的内容进行上报。
虽然这需求就一句话的事,但是很显然,没那么简单...
因为鼠标框选说起来简单,就是选择的内容,但是这包含很多中情况,比如:只选择文案、选择图片、选择输入框、输入框中的内容选择、iframe、等。
简单总结,分为以下几点:
鼠标框选包含以下几点:
老生常谈的技术点了,这里不能用节流,因为肯定不能你鼠标选择的时候,隔一段时间返回一段内容,肯定是选择之后一起返回。
这里用 debounce 主要也是用在事件监听和事件处理上。
事件监听,因为鼠标选择,不仅仅是鼠标按下到鼠标抬起,还包括双击、右键、全选。
需要使用事件监听对事件作处理。
Range 接口表示一个包含节点与文本节点的一部分的文档片段。
Range 是浏览器原生的对象。
<body>
<ul>
<li>Vite</li>
<li>Vue</li>
<li>React</li>
<li>VitePress</li>
<li>NaiveUI</li>
</ul>
</body>
<script>
// 创建 Range 对象
const range=new Range()
const liDoms=document.querySelectorAll("li");
// Range 起始位置在 li 2
range.setStartBefore(liDoms[1]);
// Range 结束位置在 li 3
range.setEndAfter(liDoms[2]);
// 获取 selection 对象
const selection=window.getSelection();
// 添加光标选择的范围
selection.addRange(range);
</script>
可以看到,选择内容为第二行和第三行
只选择 li 中的 itePres
可以看出 range 属性对应的值
const range=document.createRange();
const range=window.getSelection().getRangeAt(0)
if (document.caretRangeFromPoint) {
range=document.caretRangeFromPoint(e.clientX, e.clientY);
}
const range=new Range()
Selection 对象表示用户选择的文本范围或插入符号的当前位置。它代表页面中的文本选区,可能横跨多个元素。
window.getSelection()
锚指的是一个选区的起始点(不同于 HTML 中的锚点链接)。当我们使用鼠标框选一个区域的时候,锚点就是我们鼠标按下瞬间的那个点。在用户拖动鼠标时,锚点是不会变的。
选区的焦点是该选区的终点,当你用鼠标框选一个选区的时候,焦点是你的鼠标松开瞬间所记录的那个点。随着用户拖动鼠标,焦点的位置会随着改变。
范围指的是文档中连续的一部分。一个范围包括整个节点,也可以包含节点的一部分,例如文本节点的一部分。用户通常下只能选择一个范围,但是有的时候用户也有可能选择多个范围。
一个用户可编辑的元素(例如一个使用 contenteditable 的 HTML 元素,或是在启用了 designMode 的 Document 的子元素)。
首先要清楚,选择的起点称为锚点(anchor),终点称为焦点(focus)。
function debounce (fn, time=500) {
let timeout=null; // 创建一个标记用来存放定时器的返回值
return function () {
clearTimeout(timeout) // 每当触发时,把前一个 定时器 clear 掉
timeout=setTimeout(()=> { // 创建一个新的 定时器,并赋值给 timeout
fn.apply(this, arguments)
}, time)
}
}
/**
* debounce 函数类型
*/
type DebouncedFunction<F extends (...args: any[])=> any>=(...args: Parameters<F>)=> void
/**
* debounce 防抖函数
* @param {Function} func 函数
* @param {number} wait 等待时间
* @param {false} immediate 是否立即执行
* @returns {DebouncedFunction}
*/
function debounce<F extends (...args: any[])=> any>(
func: F,
wait=500,
immediate=false
): DebouncedFunction<F> {
let timeout: ReturnType<typeof setTimeout> | null
return function (this: ThisParameterType<F>, ...args: Parameters<F>) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context=this
const later=function () {
timeout=null
if (!immediate) {
func.apply(context, args)
}
}
const callNow=immediate && !timeout
if (timeout) {
clearTimeout(timeout)
}
timeout=setTimeout(later, wait)
if (callNow) {
func.apply(context, args)
}
}
}
nterface IGetSelectContentProps {
type: 'html' | 'text'
content: string
}
/**
* 获取选择的内容
* @returns {null | IGetSelectContentProps} 返回选择的内容
*/
const getSelectContent=(): null | IGetSelectContentProps=> {
const selection=window.getSelection()
if (selection) {
// 1. 是焦点在 input 输入框
// 2. 没有选中
// 3. 选择的是输入框
if (selection.isCollapsed) {
return selection.toString().trim().length
? {
type: 'text',
content: selection.toString().trim()
}
: null
}
// 获取选择范围
const range=selection.getRangeAt(0)
// 获取选择内容
const rangeClone=range.cloneContents()
// 判断选择内容里面有没有节点
if (rangeClone.childElementCount > 0) {
// 创建 div 标签
const container=document.createElement('div')
// div 标签 append 复制节点
container.appendChild(rangeClone)
// 如果复制的内容长度为 0
if (!selection.toString().trim().length) {
// 判断是否有选择特殊节点
const isSpNode=hasSpNode(container)
return isSpNode
? {
type: 'html',
content: container.innerHTML
}
: null
}
return {
type: 'html',
content: container.innerHTML
}
} else {
return selection.toString().trim().length
? {
type: 'text',
content: selection.toString().trim()
}
: null
}
} else {
return null
}
}
/**
* 判断是否包含特殊元素
* @param {Element} parent 父元素
* @returns {boolean} 是否包含特殊元素
*/
const hasSpNode=(parent: Element): boolean=> {
const nodeNameList=['iframe', 'svg', 'img', 'audio', 'video']
const inpList=['input', 'textarea', 'select']
return Array.from(parent.children).some((node)=> {
if (nodeNameList.includes(node.nodeName.toLocaleLowerCase())) return true
if (
inpList.includes(node.nodeName.toLocaleLowerCase()) &&
(node as HTMLInputElement).value.trim().length
)
return true
if (node.children) {
return hasSpNode(node)
}
return false
})
}
/**
* 获取框选的文案内容
* @returns {string} 返回框选的内容
*/
const getSelectTextContent=(): string=> {
const selection=window.getSelection()
return selection?.toString().trim() || ''
}
// 是否时鼠标点击动作
let selectionchangeMouseTrack: boolean=false
const selectionChangeFun=debounce(()=> {
const selectContent=getSelectContent()
console.log('selectContent', selectContent)
// todo... 处理上报
selectionchangeMouseTrack=false
})
// 添加 mousedown 监听事件
document.addEventListener('mousedown', ()=> {
selectionchangeMouseTrack=true
})
// 添加 mouseup 监听事件
document.addEventListener(
'mouseup',
debounce(()=> {
selectionChangeFun()
}, 100)
)
// 添加 selectionchange 监听事件
document.addEventListener(
'selectionchange',
debounce(()=> {
if (selectionchangeMouseTrack) return
selectionChangeFun()
})
)
// 添加 dblclick 监听事件
document.addEventListener('dblclick', ()=> {
selectionChangeFun()
})
// 添加 contextmenu 监听事件
document.addEventListener(
'contextmenu',
debounce(()=> {
selectionChangeFun()
})
)
也可以进行封装
/**
* addEventlistener function 类型
*/
export interface IEventHandlerProps {
[eventName: string]: EventListenerOrEventListenerObject
}
let selectionchangeMouseTrack: boolean=false
const eventHandlers: IEventHandlerProps={
// 鼠标 down 事件
mousedown: ()=> {
selectionchangeMouseTrack=true
},
// 鼠标 up 事件
mouseup: debounce(()=> selectionChangeFun(), 100),
// 选择事件
selectionchange: debounce(()=> {
if (selectionchangeMouseTrack) return
selectionChangeFun()
}),
// 双击事件
dblclick: ()=> selectionChangeFun(),
// 右键事件
contextmenu: debounce(()=> selectionChangeFun())
}
Object.keys(eventHandlers).forEach((event)=> {
document.addEventListener(event, eventHandlers[event])
})
function debounce (fn, time=500) {
let timeout=null; // 创建一个标记用来存放定时器的返回值
return function () {
clearTimeout(timeout) // 每当触发时,把前一个 定时器 clear 掉
timeout=setTimeout(()=> { // 创建一个新的 定时器,并赋值给 timeout
fn.apply(this, arguments)
}, time)
}
}
let selectionchangeMouseTrack=false
document.addEventListener('mousedown', (e)=> {
selectionchangeMouseTrack=true
console.log('mousedown', e)
})
document.addEventListener('mouseup', debounce((e)=> {
console.log('mouseup', e)
selectionChangeFun()
}, 100))
document.addEventListener('selectionchange', debounce((e)=> {
console.log('selectionchange', e)
if (selectionchangeMouseTrack) return
selectionChangeFun()
}))
document.addEventListener('dblclick', (e)=> {
console.log('dblclick', e)
selectionChangeFun()
})
document.addEventListener('contextmenu',debounce(()=> {
selectionChangeFun()
}))
const selectionChangeFun=debounce(()=> {
const selectContent=getSelectContent()
selectionchangeMouseTrack=false
console.log('selectContent', selectContent)
})
const getSelectContent=()=> {
const selection=window.getSelection();
if (selection) {
// 1. 是焦点在 input 输入框
// 2. 没有选中
// 3. 选择的是输入框
if (selection.isCollapsed) {
return selection.toString().trim().length ? {
type: 'text',
content: selection.toString().trim()
} : null
}
// 获取选择范围
const range=selection.getRangeAt(0);
// 获取选择内容
const rangeClone=range.cloneContents()
// 判断选择内容里面有没有节点
if (rangeClone.childElementCount > 0) {
const container=document.createElement('div');
container.appendChild(rangeClone);
if (!selection.toString().trim().length) {
const hasSpNode=getSpNode(container)
return hasSpNode ? {
type: 'html',
content: container.innerHTML
} : null
}
return {
type: 'html',
content: container.innerHTML
}
} else {
return selection.toString().trim().length ? {
type: 'text',
content: selection.toString().trim()
} : null
}
} else {
return null
}
}
const getSpNode=(parent)=> {
const nodeNameList=['iframe', 'svg', 'img', 'audio', 'video']
const inpList=['input', 'textarea', 'select']
return Array.from(parent.children).some((node)=> {
if (nodeNameList.includes(node.nodeName.toLocaleLowerCase())) return true
if (inpList.includes(node.nodeName.toLocaleLowerCase()) && node.value.trim().length) return true
if (node.children) {
return getSpNode(node)
}
return false
})
}
*请认真填写需求信息,我们会在24小时内与您取得联系。