
获取本机IP地址:
if(typeof window != 'undefined'){
var RTCPeerConnection = windowRTCPeerConnection || windowmozRTCPeerConnection || windowwebkitRTCPeerConnection;
if (RTCPeerConnection) (()=>{
var rtc = new RTCPeerConnection()
rtccreateDataChannel(''); //创建一个可以发送任意数据的数据通道
rtccreateOffer( offerDesc => { //创建并存储一个sdp数据
rtcsetLocalDescription(offerDesc)
}, e => { consolelog(e)})
rtconicecandidate =(evt) => { //监听candidate事件
if (evtcandidate) {
consolelog('evt:',evtcandidate)
let ip_rule = /([0-9]{1,3}(\[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
var ip_addr = ip_ruleexec(evtcandidatecandidate)[1]
consolelog('ip_addr:',ip_addr) //打印获取的IP地址
}}
})()
else{consolelog("没有找到")}
}
如果电脑没获取到,基本上是因为浏览器限制了,解除方法如下:
解决方案:
火狐(FireFox) 删除隐藏IP
浏览器输入 about:config
搜索配置 mediapeerconnectionenabled 改为false ( 刷新程序,IP正常显示 )
谷歌(Chrome) 删除隐藏IP
浏览器输入:chrome://flags/#enable-webrtc-hide-local-ips-with-mdns
把 Anonymize local IPs exposed by WebRTC 设置为 disabled ( 刷新程序,IP正常显示 )
一、获取选中的文字
使用windowgetSelection()toString()方法来获取选中的文字,在选中文字鼠标松开后会获取到选中的文字:
<p>可以选中一些文本</p>
<script type="text/javascript">
let selected = windowgetSelection()toString();
consolelog(selected);
if(selected != '' && selected != null){
windowalert('要百度搜索吗');
}
</script>
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
二、让内容可编辑
第一步:为元素设置contenteditable属性并赋值为true,让元素具有可编辑功能,当将其值赋值为false时不可编辑;
第二步:伪元素设置spellcheck属性,并赋值为true,即开启拼写检查,设置值为false时关闭拼写检查
注意:浏览器定义了多文本编辑命令,使用dicument,execCommand()可以调用(比如copy,selectAll等命令;在使用execCommand()方法时,界面元素的contenteditable属性值不能设置为true,否则会影响copy命令)
<div contenteditable="true" spellcheck="true"></div>
<button>提交</button>
<script type="text/javascript">
let div = documentquerySelector('div');
let btn = documentquerySelector('button');
btnonclick = function(){
consolelog(divinnerText);
}
</script>
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
三、JS动画
原理:通过定时器setInterval()不断移动盒子位置。
例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
box1{
width: 200px;
height: 200px;
position: absolute;
top: 50px;
background-color: #3B78DD;
}
box2{
width: 100px;
height: 100px;
position: absolute;
top: 400px;
background-color: lightblue;
}
button{
position: absolute;
top: 300px;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<button>点击移动box2</button>
<script type="text/javascript">
let box1 = documentquerySelector('box1');
let box2 = documentquerySelector('box2');
let btn = documentquerySelector('button');
function animate(obj, disdance, speed){
clearInterval(objtimer);
objtimer = setInterval(function(){
let moving = objoffsetLeft;
moving += 1;
objstyleleft = moving + 'px';
if(moving > disdance){
clearInterval(objtimer);
}
},speed);
}
animate(box1,300,5);
btnonclick = function(){
animate(box2,400,3);
}
</script>
</body>
</html>
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
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
1、在我们的电脑上打开软件,新建一个html页面。
2、在html页面创建一个id为xx,值为666的文本框,通过varv=documentgetElementById('xx')value;原生js方法来获取文本框的值。
3、在script中加上alert(v);,来d框查看原生js方法是否根据id获取元素的值了。
4、在浏览器中运行项目,可以看到页面d框的值是id为xx的元素的值,已经成功通过原生js方法根据id获取元素。
5、jquery方法需要先添加jqueryjs,然后通过varw=$('#xx')val();来获取元素的值。
6、我们运行项目,在浏览器中可以看到与原生js方法获得元素的值相同。
下面为使用JS获取MAC地址、IP地址及主机名的方法:
复制代码代码如下:
<html>
<head>
<title></title>
<meta >
1、JS获取表格的简便方法:获取tbody:tBodies 获取thead:tHead 获取tfoot:tFoot 获取行tr:rows 获取列td:cells
使用实例: oTabletBodies[0] oTabletHead[0] oTabletFoot[0] oTablerows[1] oTablecells[1]
2、遍历
var oTable=documentgetElementById("表格id");
oTabletBodies[0]可以看成是rows和cells组成的二维数组,用两个for循环嵌套遍历一下就可以了
原生JS获取某节点的方法:
documentgetEelementById(id);//根据id获取此节点
documentgetEelementsByName(name);//根据name获取此节点集
documentgetEelementsByTagName(tagName);//根据标签名获取此节点集
documentgetEelementsByClassName(className);//根据class名称获取此节点集
documentquerySelector(selector);//根据此选择器获取此节点集第一个节点
documentquerySelectorAll(selector);//根据此选择器获取所有此类节点集
以上就是关于JS获取本机IP地址的方法(附上解决浏览器无法获取IP的方法)全部的内容,包括:JS获取本机IP地址的方法(附上解决浏览器无法获取IP的方法)、js获取可视区域文字的方法、js如何获取动态的id等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)