工具版本兼容问题
与数字类似,基本类型String也有一个对应的String 对象,并且提供了很多有用的方法。
关键字
简介
示例代码
new String()
创建字符串对象
示例代码
属性 length
字符串长度
示例代码
方法 charAt charCodeAt
返回指定位置的字符
示例代码
方法 concat
字符串拼接
示例代码
方法 indexOf lastIndexOf
子字符串出现的位置
示例代码
方法 localeCompare
比较两段字符串是否相同
示例代码
方法 substring
截取一段子字符串
示例代码
方法 split
根据分隔符,把字符串转换为数组
示例代码
方法 replace
替换子字符串
示例代码
方法 charAt 方法 concat 方法 substring
返回基本类型
示例代码
练习
示例代码
答案
示例代码
示例
1
:
创建字符串对象
示例
2
:
字符串长度
示例
3
:
返回指定位置的字符
示例
4
:
字符串拼接
示例
5
:
子字符串出现的位置
示例
6
:
比较两段字符串是否相同
示例
7
:
截取一段子字符串
示例
8
:
根据分隔符,把字符串转换为数组
示例
9
:
替换子字符串
示例
10
:
返回基本类型
示例
11
:
练习
示例
12
:
答案
与Number类似的,可以通过new String() 创建一个String对象
");
window.frames["iframe_show1123"].document.write(decodeHtml($("textarea#stepcodeTextarea1123").val()));
window.frames["iframe_show1123"].document.close();
$(window.frames["iframe_show1123"]).load(function(){
$("#iframe_show1123").height($("#iframe_show1123").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1123").height($("#iframe_show1123").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
var x = "5";
var y = new String(x);
document.write("变量x的值是:"+x);
document.write("<br>");
document.write("变量x的类型是:"+(typeof x));
document.write("<br>");
document.write("变量y的值是:"+y);
document.write("<br>");
document.write("变量y的类型是:"+(typeof y));
document.write("<br>");
</script>
<script>
var x = "5";
var y = new String(x);
document.write("变量x的值是:"+x);
document.write("<br>");
document.write("变量x的类型是:"+(typeof x));
document.write("<br>");
document.write("变量y的值是:"+y);
document.write("<br>");
document.write("变量y的类型是:"+(typeof y));
document.write("<br>");
</script>
");
window.frames["iframe1123"].document.write(decodeHtml(code1123));
window.frames["iframe1123"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1123"]).load(function(){
$("#iframe1123").height($("#iframe1123").contents().find("body").height()+showittryitheight);
});
$("#iframe1123").height($("#iframe1123").contents().find("body").height()+showittryitheight);
alreadyWriteCode1123 = code1123;
$("#rendering1123").hide();
$("#rendered1123").show();
}
var tRereshRetry2DemoPanel1123 = setInterval(rereshRetry2DemoPanel1123,1000);
var binded1123 = false;
$("textarea#stepcodeTextarea1123").keyup(function(){
if(!binded1123){
$(window).bind('beforeunload',function(){
binded1123 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1123 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1123!=newCode){
// window.frames["iframe1123"].document.write("
");
// window.frames["iframe1123"].document.write(decodeHtml($("textarea#stepcodeTextarea1123").val()));
// window.frames["iframe1123"].document.close();
// $(window.frames["iframe1123"]).load(function(){
// $("#iframe1123").height($("#iframe1123").contents().find("body").height()+showittryitheight);
// });
// code1123 = newCode;
// }
});
$(".tryButton1123").click(function(){
$("#tryDiv1123").show();
$("#stepcodeTextarea1123").focus();
$("#stepcodeTextarea1123").height(200);
$("#iframe1123").height(0);
window.frames["iframe1123"].document.write("
");
window.frames["iframe1123"].document.write(decodeHtml($("textarea#stepcodeTextarea1123").val()));
window.frames["iframe1123"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1123"]).load(function(){
$("#iframe1123").height($("#iframe1123").contents().find("body").height()+showittryitheight);
});
$("#iframe1123").height($("#iframe1123").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1123.focus();
editor1123.setSize(null, "250");
$("#rendering1123").hide();
$("#rendered1123").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor1123 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1123"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor1123.on("change",function(doc){
if(!binded1123){
$(window).bind('beforeunload',function(){
binded1123 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1123 = newCode;
$("textarea#stepcodeTextarea1123").val(newCode);
if(alreadyWriteCode1123!=code1123){
lastModifedTime1123 = new Date().getTime();
$("#rendering1123").show();
$("#rendered1123").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1123 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1123.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1123").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
");
window.frames["iframe_show1124"].document.write(decodeHtml($("textarea#stepcodeTextarea1124").val()));
window.frames["iframe_show1124"].document.close();
$(window.frames["iframe_show1124"]).load(function(){
$("#iframe_show1124").height($("#iframe_show1124").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1124").height($("#iframe_show1124").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
var y = new String("Hello JavaScript");
document.write("通过.length属性获取字符串'Hello JavaScript'的长度"+y.length);
</script>
<script>
var y = new String("Hello JavaScript");
document.write("通过.length属性获取字符串'Hello JavaScript'的长度"+y.length);
</script>
");
window.frames["iframe1124"].document.write(decodeHtml(code1124));
window.frames["iframe1124"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1124"]).load(function(){
$("#iframe1124").height($("#iframe1124").contents().find("body").height()+showittryitheight);
});
$("#iframe1124").height($("#iframe1124").contents().find("body").height()+showittryitheight);
alreadyWriteCode1124 = code1124;
$("#rendering1124").hide();
$("#rendered1124").show();
}
var tRereshRetry2DemoPanel1124 = setInterval(rereshRetry2DemoPanel1124,1000);
var binded1124 = false;
$("textarea#stepcodeTextarea1124").keyup(function(){
if(!binded1124){
$(window).bind('beforeunload',function(){
binded1124 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1124 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1124!=newCode){
// window.frames["iframe1124"].document.write("
");
// window.frames["iframe1124"].document.write(decodeHtml($("textarea#stepcodeTextarea1124").val()));
// window.frames["iframe1124"].document.close();
// $(window.frames["iframe1124"]).load(function(){
// $("#iframe1124").height($("#iframe1124").contents().find("body").height()+showittryitheight);
// });
// code1124 = newCode;
// }
});
$(".tryButton1124").click(function(){
$("#tryDiv1124").show();
$("#stepcodeTextarea1124").focus();
$("#stepcodeTextarea1124").height(200);
$("#iframe1124").height(0);
window.frames["iframe1124"].document.write("
");
window.frames["iframe1124"].document.write(decodeHtml($("textarea#stepcodeTextarea1124").val()));
window.frames["iframe1124"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1124"]).load(function(){
$("#iframe1124").height($("#iframe1124").contents().find("body").height()+showittryitheight);
});
$("#iframe1124").height($("#iframe1124").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1124.focus();
editor1124.setSize(null, "250");
$("#rendering1124").hide();
$("#rendered1124").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor1124 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1124"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor1124.on("change",function(doc){
if(!binded1124){
$(window).bind('beforeunload',function(){
binded1124 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1124 = newCode;
$("textarea#stepcodeTextarea1124").val(newCode);
if(alreadyWriteCode1124!=code1124){
lastModifedTime1124 = new Date().getTime();
$("#rendering1124").show();
$("#rendered1124").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1124 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1124.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1124").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
charAt 返回指定位置的字符 charCodeAt 返回指定位置的字符对应的Unicode码
");
window.frames["iframe_show1125"].document.write(decodeHtml($("textarea#stepcodeTextarea1125").val()));
window.frames["iframe_show1125"].document.close();
$(window.frames["iframe_show1125"]).load(function(){
$("#iframe_show1125").height($("#iframe_show1125").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1125").height($("#iframe_show1125").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
var y = new String("Hello JavaScrpt");
document.write("字符串y的值:"+y);
document.write("<br>");
document.write('通过 charAt(0)获取位置0的字符串: '+y.charAt(0)); //返回H
document.write("<br>");
document.write('通过 charCodeAt(0)获取位置0的字符的 Unicode码 :'+y.charCodeAt(0)); //返回H对应的Unicode码 72
</script>
<script>
var y = new String("Hello JavaScrpt");
document.write("字符串y的值:"+y);
document.write("<br>");
document.write('通过 charAt(0)获取位置0的字符串: '+y.charAt(0)); //返回H
document.write("<br>");
document.write('通过 charCodeAt(0)获取位置0的字符的 Unicode码 :'+y.charCodeAt(0)); //返回H对应的Unicode码 72
</script>
");
window.frames["iframe1125"].document.write(decodeHtml(code1125));
window.frames["iframe1125"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1125"]).load(function(){
$("#iframe1125").height($("#iframe1125").contents().find("body").height()+showittryitheight);
});
$("#iframe1125").height($("#iframe1125").contents().find("body").height()+showittryitheight);
alreadyWriteCode1125 = code1125;
$("#rendering1125").hide();
$("#rendered1125").show();
}
var tRereshRetry2DemoPanel1125 = setInterval(rereshRetry2DemoPanel1125,1000);
var binded1125 = false;
$("textarea#stepcodeTextarea1125").keyup(function(){
if(!binded1125){
$(window).bind('beforeunload',function(){
binded1125 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1125 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1125!=newCode){
// window.frames["iframe1125"].document.write("
");
// window.frames["iframe1125"].document.write(decodeHtml($("textarea#stepcodeTextarea1125").val()));
// window.frames["iframe1125"].document.close();
// $(window.frames["iframe1125"]).load(function(){
// $("#iframe1125").height($("#iframe1125").contents().find("body").height()+showittryitheight);
// });
// code1125 = newCode;
// }
});
$(".tryButton1125").click(function(){
$("#tryDiv1125").show();
$("#stepcodeTextarea1125").focus();
$("#stepcodeTextarea1125").height(200);
$("#iframe1125").height(0);
window.frames["iframe1125"].document.write("
");
window.frames["iframe1125"].document.write(decodeHtml($("textarea#stepcodeTextarea1125").val()));
window.frames["iframe1125"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1125"]).load(function(){
$("#iframe1125").height($("#iframe1125").contents().find("body").height()+showittryitheight);
});
$("#iframe1125").height($("#iframe1125").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1125.focus();
editor1125.setSize(null, "250");
$("#rendering1125").hide();
$("#rendered1125").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor1125 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1125"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor1125.on("change",function(doc){
if(!binded1125){
$(window).bind('beforeunload',function(){
binded1125 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1125 = newCode;
$("textarea#stepcodeTextarea1125").val(newCode);
if(alreadyWriteCode1125!=code1125){
lastModifedTime1125 = new Date().getTime();
$("#rendering1125").show();
$("#rendered1125").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1125 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1125.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1125").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
");
window.frames["iframe_show1126"].document.write(decodeHtml($("textarea#stepcodeTextarea1126").val()));
window.frames["iframe_show1126"].document.close();
$(window.frames["iframe_show1126"]).load(function(){
$("#iframe_show1126").height($("#iframe_show1126").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1126").height($("#iframe_show1126").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
var x = new String("Hello ");
var y = new String("Javascript");
document.write( '字符串x的值: '+x);
document.write('<br>');
document.write( '字符串y的值: '+y);
document.write('<br>');
document.write( '通过函数concat()把x和y连接起来: ' + x.concat(y) );
</script>
<script>
var x = new String("Hello ");
var y = new String("Javascript");
document.write( '字符串x的值: '+x);
document.write('<br>');
document.write( '字符串y的值: '+y);
document.write('<br>');
document.write( '通过函数concat()把x和y连接起来: ' + x.concat(y) );
</script>
");
window.frames["iframe1126"].document.write(decodeHtml(code1126));
window.frames["iframe1126"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1126"]).load(function(){
$("#iframe1126").height($("#iframe1126").contents().find("body").height()+showittryitheight);
});
$("#iframe1126").height($("#iframe1126").contents().find("body").height()+showittryitheight);
alreadyWriteCode1126 = code1126;
$("#rendering1126").hide();
$("#rendered1126").show();
}
var tRereshRetry2DemoPanel1126 = setInterval(rereshRetry2DemoPanel1126,1000);
var binded1126 = false;
$("textarea#stepcodeTextarea1126").keyup(function(){
if(!binded1126){
$(window).bind('beforeunload',function(){
binded1126 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1126 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1126!=newCode){
// window.frames["iframe1126"].document.write("
");
// window.frames["iframe1126"].document.write(decodeHtml($("textarea#stepcodeTextarea1126").val()));
// window.frames["iframe1126"].document.close();
// $(window.frames["iframe1126"]).load(function(){
// $("#iframe1126").height($("#iframe1126").contents().find("body").height()+showittryitheight);
// });
// code1126 = newCode;
// }
});
$(".tryButton1126").click(function(){
$("#tryDiv1126").show();
$("#stepcodeTextarea1126").focus();
$("#stepcodeTextarea1126").height(200);
$("#iframe1126").height(0);
window.frames["iframe1126"].document.write("
");
window.frames["iframe1126"].document.write(decodeHtml($("textarea#stepcodeTextarea1126").val()));
window.frames["iframe1126"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1126"]).load(function(){
$("#iframe1126").height($("#iframe1126").contents().find("body").height()+showittryitheight);
});
$("#iframe1126").height($("#iframe1126").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1126.focus();
editor1126.setSize(null, "250");
$("#rendering1126").hide();
$("#rendered1126").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor1126 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1126"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor1126.on("change",function(doc){
if(!binded1126){
$(window).bind('beforeunload',function(){
binded1126 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1126 = newCode;
$("textarea#stepcodeTextarea1126").val(newCode);
if(alreadyWriteCode1126!=code1126){
lastModifedTime1126 = new Date().getTime();
$("#rendering1126").show();
$("#rendered1126").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1126 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1126.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1126").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
indexOf 返回子字符串第一次出现的位置
lastIndexOf 返回子字符串最后一次出现的位置
");
window.frames["iframe_show1127"].document.write(decodeHtml($("textarea#stepcodeTextarea1127").val()));
window.frames["iframe_show1127"].document.close();
$(window.frames["iframe_show1127"]).load(function(){
$("#iframe_show1127").height($("#iframe_show1127").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1127").height($("#iframe_show1127").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
var y = new String("Hello JavaScript");
document.write( '字符串y的值: '+y);
document.write('<br>');
document.write('通过 indexOf ("a")获取子字符"a" 第一次出现的位置 '+y.indexOf ("a"));
document.write('<br>');
document.write('通过 lastIndexOf ("a")获取子字符"a" 最后出现的位置 '+y.lastIndexOf ("a"));
</script>
<script>
var y = new String("Hello JavaScript");
document.write( '字符串y的值: '+y);
document.write('<br>');
document.write('通过 indexOf ("a")获取子字符"a" 第一次出现的位置 '+y.indexOf ("a"));
document.write('<br>');
document.write('通过 lastIndexOf ("a")获取子字符"a" 最后出现的位置 '+y.lastIndexOf ("a"));
</script>
");
window.frames["iframe1127"].document.write(decodeHtml(code1127));
window.frames["iframe1127"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1127"]).load(function(){
$("#iframe1127").height($("#iframe1127").contents().find("body").height()+showittryitheight);
});
$("#iframe1127").height($("#iframe1127").contents().find("body").height()+showittryitheight);
alreadyWriteCode1127 = code1127;
$("#rendering1127").hide();
$("#rendered1127").show();
}
var tRereshRetry2DemoPanel1127 = setInterval(rereshRetry2DemoPanel1127,1000);
var binded1127 = false;
$("textarea#stepcodeTextarea1127").keyup(function(){
if(!binded1127){
$(window).bind('beforeunload',function(){
binded1127 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1127 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1127!=newCode){
// window.frames["iframe1127"].document.write("
");
// window.frames["iframe1127"].document.write(decodeHtml($("textarea#stepcodeTextarea1127").val()));
// window.frames["iframe1127"].document.close();
// $(window.frames["iframe1127"]).load(function(){
// $("#iframe1127").height($("#iframe1127").contents().find("body").height()+showittryitheight);
// });
// code1127 = newCode;
// }
});
$(".tryButton1127").click(function(){
$("#tryDiv1127").show();
$("#stepcodeTextarea1127").focus();
$("#stepcodeTextarea1127").height(200);
$("#iframe1127").height(0);
window.frames["iframe1127"].document.write("
");
window.frames["iframe1127"].document.write(decodeHtml($("textarea#stepcodeTextarea1127").val()));
window.frames["iframe1127"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1127"]).load(function(){
$("#iframe1127").height($("#iframe1127").contents().find("body").height()+showittryitheight);
});
$("#iframe1127").height($("#iframe1127").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1127.focus();
editor1127.setSize(null, "250");
$("#rendering1127").hide();
$("#rendered1127").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor1127 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1127"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor1127.on("change",function(doc){
if(!binded1127){
$(window).bind('beforeunload',function(){
binded1127 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1127 = newCode;
$("textarea#stepcodeTextarea1127").val(newCode);
if(alreadyWriteCode1127!=code1127){
lastModifedTime1127 = new Date().getTime();
$("#rendering1127").show();
$("#rendered1127").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1127 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1127.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1127").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
localeCompare 比较两段字符串是否相同,0即表示相同,非0表示不同
");
window.frames["iframe_show1128"].document.write(decodeHtml($("textarea#stepcodeTextarea1128").val()));
window.frames["iframe_show1128"].document.close();
$(window.frames["iframe_show1128"]).load(function(){
$("#iframe_show1128").height($("#iframe_show1128").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1128").height($("#iframe_show1128").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
var x = new String("Hello");
var y = new String("Hello");
var z = new String("aloha");
document.write( '字符串x的值: '+x);
document.write('<br>');
document.write( '字符串y的值: '+y);
document.write('<br>');
document.write( '字符串z的值: '+z);
document.write('<br>');
document.write('通过 localeCompare()判断 x和y是否相等 '+x.localeCompare(y));
document.write('<br>');
document.write('通过 localeCompare()判断 x和z是否相等 '+x.localeCompare(z));
document.write('<br>');
document.write('0 表示相等<br>');
document.write('1 表示字母顺序靠后<br>');
document.write('-1 表示字母顺序靠前<br>');
</script>
<script>
var x = new String("Hello");
var y = new String("Hello");
var z = new String("aloha");
document.write( '字符串x的值: '+x);
document.write('<br>');
document.write( '字符串y的值: '+y);
document.write('<br>');
document.write( '字符串z的值: '+z);
document.write('<br>');
document.write('通过 localeCompare()判断 x和y是否相等 '+x.localeCompare(y));
document.write('<br>');
document.write('通过 localeCompare()判断 x和z是否相等 '+x.localeCompare(z));
document.write('<br>');
document.write('0 表示相等<br>');
document.write('1 表示字母顺序靠后<br>');
document.write('-1 表示字母顺序靠前<br>');
</script>
");
window.frames["iframe1128"].document.write(decodeHtml(code1128));
window.frames["iframe1128"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1128"]).load(function(){
$("#iframe1128").height($("#iframe1128").contents().find("body").height()+showittryitheight);
});
$("#iframe1128").height($("#iframe1128").contents().find("body").height()+showittryitheight);
alreadyWriteCode1128 = code1128;
$("#rendering1128").hide();
$("#rendered1128").show();
}
var tRereshRetry2DemoPanel1128 = setInterval(rereshRetry2DemoPanel1128,1000);
var binded1128 = false;
$("textarea#stepcodeTextarea1128").keyup(function(){
if(!binded1128){
$(window).bind('beforeunload',function(){
binded1128 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1128 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1128!=newCode){
// window.frames["iframe1128"].document.write("
");
// window.frames["iframe1128"].document.write(decodeHtml($("textarea#stepcodeTextarea1128").val()));
// window.frames["iframe1128"].document.close();
// $(window.frames["iframe1128"]).load(function(){
// $("#iframe1128").height($("#iframe1128").contents().find("body").height()+showittryitheight);
// });
// code1128 = newCode;
// }
});
$(".tryButton1128").click(function(){
$("#tryDiv1128").show();
$("#stepcodeTextarea1128").focus();
$("#stepcodeTextarea1128").height(200);
$("#iframe1128").height(0);
window.frames["iframe1128"].document.write("
");
window.frames["iframe1128"].document.write(decodeHtml($("textarea#stepcodeTextarea1128").val()));
window.frames["iframe1128"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1128"]).load(function(){
$("#iframe1128").height($("#iframe1128").contents().find("body").height()+showittryitheight);
});
$("#iframe1128").height($("#iframe1128").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1128.focus();
editor1128.setSize(null, "250");
$("#rendering1128").hide();
$("#rendered1128").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor1128 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1128"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor1128.on("change",function(doc){
if(!binded1128){
$(window).bind('beforeunload',function(){
binded1128 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1128 = newCode;
$("textarea#stepcodeTextarea1128").val(newCode);
if(alreadyWriteCode1128!=code1128){
lastModifedTime1128 = new Date().getTime();
$("#rendering1128").show();
$("#rendered1128").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1128 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1128.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1128").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
substring 截取一段子字符串注: 第二个参数,取不到
");
window.frames["iframe_show1129"].document.write(decodeHtml($("textarea#stepcodeTextarea1129").val()));
window.frames["iframe_show1129"].document.close();
$(window.frames["iframe_show1129"]).load(function(){
$("#iframe_show1129").height($("#iframe_show1129").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1129").height($("#iframe_show1129").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
var x = new String("Hello JavaScript");
document.write( '字符串x的值: '+x);
document.write('<br>');
document.write('x.substring (0,3) 获取位0到3的字符串: '+x.substring (0,3) );
document.write('<br>');
document.write('左闭右开,取得到0,取不到3');
</script>
<script>
var x = new String("Hello JavaScript");
document.write( '字符串x的值: '+x);
document.write('<br>');
document.write('x.substring (0,3) 获取位0到3的字符串: '+x.substring (0,3) );
document.write('<br>');
document.write('左闭右开,取得到0,取不到3');
</script>
");
window.frames["iframe1129"].document.write(decodeHtml(code1129));
window.frames["iframe1129"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1129"]).load(function(){
$("#iframe1129").height($("#iframe1129").contents().find("body").height()+showittryitheight);
});
$("#iframe1129").height($("#iframe1129").contents().find("body").height()+showittryitheight);
alreadyWriteCode1129 = code1129;
$("#rendering1129").hide();
$("#rendered1129").show();
}
var tRereshRetry2DemoPanel1129 = setInterval(rereshRetry2DemoPanel1129,1000);
var binded1129 = false;
$("textarea#stepcodeTextarea1129").keyup(function(){
if(!binded1129){
$(window).bind('beforeunload',function(){
binded1129 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1129 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1129!=newCode){
// window.frames["iframe1129"].document.write("
");
// window.frames["iframe1129"].document.write(decodeHtml($("textarea#stepcodeTextarea1129").val()));
// window.frames["iframe1129"].document.close();
// $(window.frames["iframe1129"]).load(function(){
// $("#iframe1129").height($("#iframe1129").contents().find("body").height()+showittryitheight);
// });
// code1129 = newCode;
// }
});
$(".tryButton1129").click(function(){
$("#tryDiv1129").show();
$("#stepcodeTextarea1129").focus();
$("#stepcodeTextarea1129").height(200);
$("#iframe1129").height(0);
window.frames["iframe1129"].document.write("
");
window.frames["iframe1129"].document.write(decodeHtml($("textarea#stepcodeTextarea1129").val()));
window.frames["iframe1129"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1129"]).load(function(){
$("#iframe1129").height($("#iframe1129").contents().find("body").height()+showittryitheight);
});
$("#iframe1129").height($("#iframe1129").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1129.focus();
editor1129.setSize(null, "250");
$("#rendering1129").hide();
$("#rendered1129").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor1129 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1129"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor1129.on("change",function(doc){
if(!binded1129){
$(window).bind('beforeunload',function(){
binded1129 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1129 = newCode;
$("textarea#stepcodeTextarea1129").val(newCode);
if(alreadyWriteCode1129!=code1129){
lastModifedTime1129 = new Date().getTime();
$("#rendering1129").show();
$("#rendered1129").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1129 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1129.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1129").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
示例
8
:
根据分隔符,把字符串转换为数组
顶
折
split 根据分隔符,把字符串转换为数组。
注: 第二个参数可选,表示返回数组得长度
");
window.frames["iframe_show1205"].document.write(decodeHtml($("textarea#stepcodeTextarea1205").val()));
window.frames["iframe_show1205"].document.close();
$(window.frames["iframe_show1205"]).load(function(){
$("#iframe_show1205").height($("#iframe_show1205").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1205").height($("#iframe_show1205").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
var x = new String("Hello This Is JavaScript");
document.write( '字符串x的值: '+x);
document.write('<br>');
var y = x.split(" ");
document.write('通过空格分隔split(" "),得到数组'+y);
document.write("<br>");
var z = x.split(" ",2);
document.write('通过空格分隔split(" ",2),得到数组,并且只保留前两个'+z);
</script>
<script>
var x = new String("Hello This Is JavaScript");
document.write( '字符串x的值: '+x);
document.write('<br>');
var y = x.split(" ");
document.write('通过空格分隔split(" "),得到数组'+y);
document.write("<br>");
var z = x.split(" ",2);
document.write('通过空格分隔split(" ",2),得到数组,并且只保留前两个'+z);
</script>
");
window.frames["iframe1205"].document.write(decodeHtml(code1205));
window.frames["iframe1205"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1205"]).load(function(){
$("#iframe1205").height($("#iframe1205").contents().find("body").height()+showittryitheight);
});
$("#iframe1205").height($("#iframe1205").contents().find("body").height()+showittryitheight);
alreadyWriteCode1205 = code1205;
$("#rendering1205").hide();
$("#rendered1205").show();
}
var tRereshRetry2DemoPanel1205 = setInterval(rereshRetry2DemoPanel1205,1000);
var binded1205 = false;
$("textarea#stepcodeTextarea1205").keyup(function(){
if(!binded1205){
$(window).bind('beforeunload',function(){
binded1205 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1205 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1205!=newCode){
// window.frames["iframe1205"].document.write("
");
// window.frames["iframe1205"].document.write(decodeHtml($("textarea#stepcodeTextarea1205").val()));
// window.frames["iframe1205"].document.close();
// $(window.frames["iframe1205"]).load(function(){
// $("#iframe1205").height($("#iframe1205").contents().find("body").height()+showittryitheight);
// });
// code1205 = newCode;
// }
});
$(".tryButton1205").click(function(){
$("#tryDiv1205").show();
$("#stepcodeTextarea1205").focus();
$("#stepcodeTextarea1205").height(200);
$("#iframe1205").height(0);
window.frames["iframe1205"].document.write("
");
window.frames["iframe1205"].document.write(decodeHtml($("textarea#stepcodeTextarea1205").val()));
window.frames["iframe1205"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1205"]).load(function(){
$("#iframe1205").height($("#iframe1205").contents().find("body").height()+showittryitheight);
});
$("#iframe1205").height($("#iframe1205").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1205.focus();
editor1205.setSize(null, "250");
$("#rendering1205").hide();
$("#rendered1205").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor1205 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1205"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor1205.on("change",function(doc){
if(!binded1205){
$(window).bind('beforeunload',function(){
binded1205 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1205 = newCode;
$("textarea#stepcodeTextarea1205").val(newCode);
if(alreadyWriteCode1205!=code1205){
lastModifedTime1205 = new Date().getTime();
$("#rendering1205").show();
$("#rendered1205").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1205 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1205.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1205").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
replace(search,replacement)
找到满足条件的子字符串search,替换为replacement
注: 默认情况下只替换找到的第一个子字符串,如果要所有都替换,需要写成:
x.replace(/a/g, "o"); 或者 var regS = new RegExp("a","g"); x.replace(regS, "o");
");
window.frames["iframe_show1814"].document.write(decodeHtml($("textarea#stepcodeTextarea1814").val()));
window.frames["iframe_show1814"].document.close();
$(window.frames["iframe_show1814"]).load(function(){
$("#iframe_show1814").height($("#iframe_show1814").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1814").height($("#iframe_show1814").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
function p(s){
document.write(s);
document.write("<br>");
}
var x = new String("Hello JavaScript");
p('这个是原字符串: '+x);
var y = x.replace("a","o");
p('只替换第一个 a: '+y);
var regS = new RegExp("a","g");
var z = x.replace(regS, "o");
p('替换掉所有的 a: '+z);
</script>
<script>
function p(s){
document.write(s);
document.write("<br>");
}
var x = new String("Hello JavaScript");
p('这个是原字符串: '+x);
var y = x.replace("a","o");
p('只替换第一个 a: '+y);
var regS = new RegExp("a","g");
var z = x.replace(regS, "o");
p('替换掉所有的 a: '+z);
</script>
");
window.frames["iframe1814"].document.write(decodeHtml(code1814));
window.frames["iframe1814"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1814"]).load(function(){
$("#iframe1814").height($("#iframe1814").contents().find("body").height()+showittryitheight);
});
$("#iframe1814").height($("#iframe1814").contents().find("body").height()+showittryitheight);
alreadyWriteCode1814 = code1814;
$("#rendering1814").hide();
$("#rendered1814").show();
}
var tRereshRetry2DemoPanel1814 = setInterval(rereshRetry2DemoPanel1814,1000);
var binded1814 = false;
$("textarea#stepcodeTextarea1814").keyup(function(){
if(!binded1814){
$(window).bind('beforeunload',function(){
binded1814 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1814 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1814!=newCode){
// window.frames["iframe1814"].document.write("
");
// window.frames["iframe1814"].document.write(decodeHtml($("textarea#stepcodeTextarea1814").val()));
// window.frames["iframe1814"].document.close();
// $(window.frames["iframe1814"]).load(function(){
// $("#iframe1814").height($("#iframe1814").contents().find("body").height()+showittryitheight);
// });
// code1814 = newCode;
// }
});
$(".tryButton1814").click(function(){
$("#tryDiv1814").show();
$("#stepcodeTextarea1814").focus();
$("#stepcodeTextarea1814").height(200);
$("#iframe1814").height(0);
window.frames["iframe1814"].document.write("
");
window.frames["iframe1814"].document.write(decodeHtml($("textarea#stepcodeTextarea1814").val()));
window.frames["iframe1814"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1814"]).load(function(){
$("#iframe1814").height($("#iframe1814").contents().find("body").height()+showittryitheight);
});
$("#iframe1814").height($("#iframe1814").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1814.focus();
editor1814.setSize(null, "250");
$("#rendering1814").hide();
$("#rendered1814").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor1814 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1814"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor1814.on("change",function(doc){
if(!binded1814){
$(window).bind('beforeunload',function(){
binded1814 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1814 = newCode;
$("textarea#stepcodeTextarea1814").val(newCode);
if(alreadyWriteCode1814!=code1814){
lastModifedTime1814 = new Date().getTime();
$("#rendering1814").show();
$("#rendered1814").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1814 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1814.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1814").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
需要注意的是,所有返回字符串类型 的方法,返回的都是基本类型的String
");
window.frames["iframe_show1130"].document.write(decodeHtml($("textarea#stepcodeTextarea1130").val()));
window.frames["iframe_show1130"].document.close();
$(window.frames["iframe_show1130"]).load(function(){
$("#iframe_show1130").height($("#iframe_show1130").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1130").height($("#iframe_show1130").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
function p(s){
document.write(s);
document.write("<br>");
}
var x = new String("Hello JavaScript");
var temp = x.charAt(0);
p('charAt返回的值'+temp);
p('其类型是'+ typeof temp );
var temp = x.concat("!!!");
p('concat返回的值'+temp);
p('其类型是'+ typeof temp );
var temp = x.substring(0,5);
p('substring返回的值'+temp);
p('其类型是'+ typeof temp );
</script>
<script>
function p(s){
document.write(s);
document.write("<br>");
}
var x = new String("Hello JavaScript");
var temp = x.charAt(0);
p('charAt返回的值'+temp);
p('其类型是'+ typeof temp );
var temp = x.concat("!!!");
p('concat返回的值'+temp);
p('其类型是'+ typeof temp );
var temp = x.substring(0,5);
p('substring返回的值'+temp);
p('其类型是'+ typeof temp );
</script>
");
window.frames["iframe1130"].document.write(decodeHtml(code1130));
window.frames["iframe1130"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1130"]).load(function(){
$("#iframe1130").height($("#iframe1130").contents().find("body").height()+showittryitheight);
});
$("#iframe1130").height($("#iframe1130").contents().find("body").height()+showittryitheight);
alreadyWriteCode1130 = code1130;
$("#rendering1130").hide();
$("#rendered1130").show();
}
var tRereshRetry2DemoPanel1130 = setInterval(rereshRetry2DemoPanel1130,1000);
var binded1130 = false;
$("textarea#stepcodeTextarea1130").keyup(function(){
if(!binded1130){
$(window).bind('beforeunload',function(){
binded1130 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1130 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1130!=newCode){
// window.frames["iframe1130"].document.write("
");
// window.frames["iframe1130"].document.write(decodeHtml($("textarea#stepcodeTextarea1130").val()));
// window.frames["iframe1130"].document.close();
// $(window.frames["iframe1130"]).load(function(){
// $("#iframe1130").height($("#iframe1130").contents().find("body").height()+showittryitheight);
// });
// code1130 = newCode;
// }
});
$(".tryButton1130").click(function(){
$("#tryDiv1130").show();
$("#stepcodeTextarea1130").focus();
$("#stepcodeTextarea1130").height(200);
$("#iframe1130").height(0);
window.frames["iframe1130"].document.write("
");
window.frames["iframe1130"].document.write(decodeHtml($("textarea#stepcodeTextarea1130").val()));
window.frames["iframe1130"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1130"]).load(function(){
$("#iframe1130").height($("#iframe1130").contents().find("body").height()+showittryitheight);
});
$("#iframe1130").height($("#iframe1130").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1130.focus();
editor1130.setSize(null, "250");
$("#rendering1130").hide();
$("#rendered1130").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor1130 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1130"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor1130.on("change",function(doc){
if(!binded1130){
$(window).bind('beforeunload',function(){
binded1130 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1130 = newCode;
$("textarea#stepcodeTextarea1130").val(newCode);
if(alreadyWriteCode1130!=code1130){
lastModifedTime1130 = new Date().getTime();
$("#rendering1130").show();
$("#rendered1130").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1130 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1130.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1130").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
");
window.frames["iframe_show2110"].document.write(decodeHtml($("textarea#stepcodeTextarea2110").val()));
window.frames["iframe_show2110"].document.close();
$(window.frames["iframe_show2110"]).load(function(){
$("#iframe_show2110").height($("#iframe_show2110").contents().find("body").height()+showittryitheight);
});
$("#iframe_show2110").height($("#iframe_show2110").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
代码比较
复制代码
<script>
function getValue(id){
return document.getElementById(id).value;
}
function setValue(id,value){
document.getElementById(id).value=value;
}
function replace(){
var src = getValue("src")
var search = getValue("search")
var replace = getValue("replace")
var regS = new RegExp(search,"g");
var result = src.replace(regS, replace);
setValue("result",result);
}
</script>
<table>
<tr>
<td>源字符串:</td>
<td><textarea id="src">example</textarea></td>
</tr>
<tr>
<td>查询:</td>
<td><input type="text" id="search" value="a" ></td>
</tr>
<tr>
<td>替换为:</td>
<td><input type="text" id="replace" value="e" ></td>
</tr>
<tr>
<td>替换结果:</td>
<td><textarea id="result"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><button onclick="replace()">替换</button></td>
<td></td>
</tr>
</table>
<script>
function getValue(id){
return document.getElementById(id).value;
}
function setValue(id,value){
document.getElementById(id).value=value;
}
function replace(){
var src = getValue("src")
var search = getValue("search")
var replace = getValue("replace")
var regS = new RegExp(search,"g");
var result = src.replace(regS, replace);
setValue("result",result);
}
</script>
<table>
<tr>
<td>源字符串:</td>
<td><textarea id="src">example</textarea></td>
</tr>
<tr>
<td>查询:</td>
<td><input type="text" id="search" value="a" ></td>
</tr>
<tr>
<td>替换为:</td>
<td><input type="text" id="replace" value="e" ></td>
</tr>
<tr>
<td>替换结果:</td>
<td><textarea id="result"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><button onclick="replace()">替换</button></td>
<td></td>
</tr>
</table>
");
window.frames["iframe2110"].document.write(decodeHtml(code2110));
window.frames["iframe2110"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe2110"]).load(function(){
$("#iframe2110").height($("#iframe2110").contents().find("body").height()+showittryitheight);
});
$("#iframe2110").height($("#iframe2110").contents().find("body").height()+showittryitheight);
alreadyWriteCode2110 = code2110;
$("#rendering2110").hide();
$("#rendered2110").show();
}
var tRereshRetry2DemoPanel2110 = setInterval(rereshRetry2DemoPanel2110,1000);
var binded2110 = false;
$("textarea#stepcodeTextarea2110").keyup(function(){
if(!binded2110){
$(window).bind('beforeunload',function(){
binded2110 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code2110 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code2110!=newCode){
// window.frames["iframe2110"].document.write("
");
// window.frames["iframe2110"].document.write(decodeHtml($("textarea#stepcodeTextarea2110").val()));
// window.frames["iframe2110"].document.close();
// $(window.frames["iframe2110"]).load(function(){
// $("#iframe2110").height($("#iframe2110").contents().find("body").height()+showittryitheight);
// });
// code2110 = newCode;
// }
});
$(".tryButton2110").click(function(){
$("#tryDiv2110").show();
$("#stepcodeTextarea2110").focus();
$("#stepcodeTextarea2110").height(200);
$("#iframe2110").height(0);
window.frames["iframe2110"].document.write("
");
window.frames["iframe2110"].document.write(decodeHtml($("textarea#stepcodeTextarea2110").val()));
window.frames["iframe2110"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe2110"]).load(function(){
$("#iframe2110").height($("#iframe2110").contents().find("body").height()+showittryitheight);
});
$("#iframe2110").height($("#iframe2110").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor2110.focus();
editor2110.setSize(null, "250");
$("#rendering2110").hide();
$("#rendered2110").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor2110 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea2110"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor2110.on("change",function(doc){
if(!binded2110){
$(window).bind('beforeunload',function(){
binded2110 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code2110 = newCode;
$("textarea#stepcodeTextarea2110").val(newCode);
if(alreadyWriteCode2110!=code2110){
lastModifedTime2110 = new Date().getTime();
$("#rendering2110").show();
$("#rendered2110").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor2110 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor2110.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv2110").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
查看答案
请先登录
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆