var selectText = documentgetElementById("sect")options[selectIndex]text //获得被选中的项目的文本,即显示值
一、获取选中的文字
使用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
在你的代码后添加下面代码,就可以在修改select选择后,得到每个select中的值了。
<div id="output_div"></div>
<script type="text/javascript">
$(function(){
output_func();
$("select")change(output_func);
});
function output_func(){
var str="输出显示为:<br>";
$("tr")each(function(){
$(this)find("td")each(function(){
str+="<span style='border:1px solid blue;'>"+$(this)find("select")val()+"</span> ";
});
str+="<br>";
});
$("#output_div")html("<br>" + str);
}
</script>
var opt=documentgetElementById("cmbProvince")getElementsByTagName("option");
for(var i=0;i<optlength;i++){
if(opt[i]innerHTML=="云南省")opt[i]selected=true;
}
如果是jq可以这样:
$("#cmbProvince option:contains(云南省)")prop("selected",true);<select id="fruit">
<option value="apple">苹果</option>
<option value="pear">梨子</option>
<option value="peach">桃子</option>
<option value="banana">香蕉</option>
</select>
取值:
documentgetElementById('fruit')value;1、获取选中select的value和text,html代码如下:
<select id="mySelect">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
则可通过以下script代码s来获取选中的value和text
$("#mySelect")val(); //获取选中记录的value值
$("#mySelect option:selected")text(); //获取选中记录的text值
2、运用new Option("文本","值")方法添加选项option
var obj = documentgetElementById("mySelect");
objadd(new Option("4","4"));
3、删除所有选项option
var obj = documentgetElementById("mySelect");
objoptionslength = 0;
4、删除选中选项option
var obj = documentgetElementById("mySelect");
var index = objselectedIndex;
objoptionsremove(index);
5、修改选中选项option
var obj = documentgetElementById("mySelect");
var index = objselectedIndex;
objoptions[index] = new Option("three",3); //更改对应的值
objoptions[index]selected = true; //保持选中状态
6、删除select
var obj = documentgetElementById("mySelect");
objparentNoderemoveChild(obj); //移除当前对象
7、select选择的响应事件
$("#mySelect")change(function(){
//添加所需要执行的 *** 作代码
})
以上就是关于js怎么获取select的显示值全部的内容,包括:js怎么获取select的显示值、js获取可视区域文字的方法、js获取jsp页面表格内select选中的值得问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)