stringObject.indexOf(searchvalue,fromindex)
参数 | 描述 |
searchvalue | 必需。规定需检索的字符串值。 |
fromindex | 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。 |
注释:indexOf() 方法对大小写敏感!
注释:没找到就返回 -1。
实例
<script type="text/javascript">
var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")
document.write(str.indexOf("World") + "<br />")
</script>
以上代码的输出:
0
-1
另:大小写字母转换
stringObject.toUpperCase();
stringObject.toLowerCase();
1.丢弃小数部分,保留整数部分
js:parseInt(7/2) = 3
2.向上取整,有小数就整数部分加1
js: Math.ceil(7/2) = 4
3,四舍五入.
js: Math.round(7/2) = 4
4,向下取整
js: Math.floor(7/2) = 3
function domToStr(dom) {
var div = document.createElement("div");
div.appendChild(dom);
return div.innerHTML;
}
将DOM对象添加至新DIV内,返回其innerHTML即可,innerHTML是字符串类型。
function strToDom (htmlStr) {
var div = document.createElement("div");
div.innerHTML = html;
return div.children[0];
}
另:
var text = "<div id='overLay' style='width:50px;height:60px;background:url(imgs/back.png) left top no-repeat; position: absolute;'>"
+ "<img style='margin-left:4px;margin-top: 3px;' src='ima.png' width='43px' height='43px'/>"
+ "</div>";
alert($(text).html());
输出:<img style='margin-left:4px;margin-top: 3px;' src='ima.png' width='43px' height='43px'/>
$("#test").html() 意思是指:获取ID为test的元素内的html代码。其中html()是jQuery里的方法。