首页 > web前端 > js教程 > 正文

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

巴扎黑
发布: 2016-11-25 10:32:46
原创
1199人浏览过

<%@ 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>

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

腾讯元宝 223
查看详情 腾讯元宝
相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号