补充知识点

目录

一、JSON对象和字符串的相互转换

二、insertAdjacentHTML()

三、选中文字


一、JSON对象和字符串的相互转换

//json字符串转换为js对象
var json='{"a":"hello","b":"world"}'
var obj1=JSON.parse(json); 
console.log(obj1); //{a:'hello',b:'world'}

//js对象转换为json字符串
var obj={a:'hello',b:'world'};
var json2=JSON.stringify(obj);
console.log(json2); //'{"a":"hello","b":"world"}'

二、insertAdjacentHTML()

利用insertAdjacentHTML(position,text)可以直接把字符串元素添加到父元素中

注意:appendChild()不支持追加字符串子元素, insertAdjacentHTML支持追加字符串元素

position: 表示追加的位置

        ‘beforeBegin’ 元素自身前面

        ‘afterEnd’元素自身后面

        ’afterBegin‘元素内部前面

        ‘beforeEnd’元素内部后面

text: 表示追加的html字符串

var mydiv=document.querySelector('div');
mydiv.insertAdjacentHTML('beforeEnd','<h3>insertAdjacentHTML的使用</h3>');

三、选中文字

双击禁止选中文字:

    window.getSelection?window.getSelection().removeAllRanges():document.selection.empty();

文本框文字处于选中状态:

    select()


猜你喜欢

转载自blog.csdn.net/Primary_Insist/article/details/123169272