js创建元素和6种是true的情况

js教程评论369 views阅读模式

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%

String path= request.getContextPath();

String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<base href="<%=basePath%>"/>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>

<title>生成html</title>

<script type="text/javascript">

$(function(){

alert("!null=="+!null);//true

alert("!undefined=="+!undefined);//true

alert("!false=="+!false);//true

alert("!0=="+!0);//true

alert("!-0=="+!-0);//true

alert("!''=="+!"");//true

debug("调试信息");

ff("风阻了街道");

function debug(msg){

//通过ID获取元素

var log=document.getElementById("debuglog");

//判断获取到的标签是否存在

alert("1--->"+!log);//true

if(!log){

//元素不存在的情况下,则创建一个

log=document.createElement("div");

log.id="debuglog";

log.innerHTML="<h1>Debug Log</h1>";

//将创建的div添加到页面中

document.body.appendChild(log);

}

//在找的元素存在的情况下,将msg消息包装在<pre>中。并添加至log中

var pre=document.createElement("pre");

//创建<pre>标签要显示的文本对象

var text=document.createTextNode(msg);

//将<pre>要显示的文本对象添加到<pre>标签中

pre.appendChild(text);

//将<pre>标签添加到<div>中

log.appendChild(pre);

}

//使用jquery进行实现

function ff(msg){

var log=$("#debug");

alert("2--->"+!log);//false,原因是通过jquery获取元素的时候,相当于创建一个jquery对象,即相当于new,!object是false

if(log){

log=$("<div id='debug'></div>");

alert(log);

log.html("<h1>调试信息如下:</h1>");

$("body").append(log);

}

var pre= $("<pre></pre>");

pre.append(msg);

log.append(pre);

}

});

</script>

</head>

<body>

</body>

</html>

企鹅博客
  • 本文由 发表于 2019年9月22日 22:13:32
  • 转载请务必保留本文链接:https://www.qieseo.com/386102.html

发表评论