day17

DOM操作获取节点

DOM(document object model) 操作
js提供了一个document,指向的是html,在js中通过document对象 可以直接或者间接获取页面内容对应的标签。

DOM操作具体内容 - 操作节点(节点就是标签)
a.直接获取节点 - document.getElementId(id属性值) - 通过id属性值获取标签
getElementByClassName(class属性值)
getElementsByName(标签名)
b.获取父节点:节点.parentElement - 获取指定节点的父节点
c.获取子节点:节点.children - 获取指定节点的所有子节点
节点.firstElementchild - 获取指定节点的第一个子节点
节点.lastElementchild - 获取指定节点的最后一个子节点
d.添加节点
e.删除节点

	<body>
		<div id = "box1">
			<p id = "p1">段落</p>
		</div>
		<div id = "box3">
			<a href="">百度</a>
			<p id = "p2">段落</p>
		</div>
		<div id = "box3">
			<p id = "p3">段落</p>
		</div>
				
		<script type="text/javascript">
			p1 = document.getElementById('p1')
			console.log(p1)
			p1.style.color = 'red'
			//p1.style = 'color:red;'
				
			p2 = document.getElementsByTagName('p')
			console.log(p2)
				
			colors = ['blue','green','pink']
			for(x in p2){
				p2[x].style = `color:${colors[x]};`
			}	
						
			div1 = document.getElementById('p1').parentElement
			console.log(div1)			
		</script>
	</body>

DOM操作添加节点

1.创建新的节点
document.createElement(标签名)

2.添加节点
节点1.appendChild(节点2) - 在节点1的最后添加节点2(节点1和节点2是父子)
节点2.insertBefore(节点2,节点3) = 在节点1中节点3的前边添加节点2

	<body>
		<script>
			document.write('<span>aaaa</span>')
		</script>
		<div id = "div1">
			<p>段落</p>
		</div>
		
		<script>
			//直接向body标签添加子标签
			document.write('<span>aaaa</span>')
		</script>
        
		<script>
			//向P标签前添加新标签
			a = document.createElement('a')
			p = document.getElementById('p1')
			div1 = document.getElementById('div1')
			.insertBefore(a,p)
		</script>
	</body>

DOM操作删除节点

	<body>
		<p id= "p1">段落1</p>
		<p>段落2</p>
		<p>段落3</p>
	</body>
	
	<script>
		//remove() - 删除节点
		//节点.remove()
		p1 = document.getElementById('p1')
		p1.remove()
		
		p = document.getElementsByName('p')
		console.log(p)
		p[1].remove()
	</script>

DOM操作向节点中添加内容和属性

节点.innerText – 标签内部的文字(子标签也属于文字)
节点.innerHTML – 标签内部的文字(包括子标签内部的文字)

	<body>
		<a class = "a1">百度<b>一下</b></a>
		
		<script>
			a1 = document.getElementsByTagName('a')
			
			//a1[0].innerText = 'HTML'
			//a1[0].innerText = 'HTML<b>CSS</b>'
			//a1[0].innerHTML = 'HTML'
			a1[0].innerHTML = 'HTML<b>CSS</b>'
			
			//节点.属性 = 属性值 ---> 向节点内添加或修改属性
			a1[0].href = 'http://www.baidu.com'
			a1[0].target = '_blank'
			a1[0].className = 'a2'
			
		</script>
	</body>

js操作浏览器

  • 打开关闭标签页

    			//打开浏览器新标签页:window.open()
    			function openwindow1(){
    				w1 = window.open('https://www.baidu.com')
    			}
    			//关闭刚打开的新标签页
    			function close1(){
    				w1.close()
    			}
    		
    		<input type="submit" value="打开新窗口" onclick="openwindow2()">
    		<input type="submit" value="关闭窗口" onclick="close2()">
    
  • 打开关闭窗口

    			//打开新窗口
    			function openwindow2(){
    				w2 = window.open('https://www.baidu.com','_blank','width=800,height=800')
    				w2.moveTo(500,200)
    			}
    			
    			function close2(){
    				w2.close()
    			}
    			
    			//关掉当前窗口
    			function close3(){
    				window.close()
    			}
    		<input type="submit" value="打开新标签页" onclick="openwindow1()">
    		<input type="submit" value="关闭标签页" onclick="close1()">
    		<input type="submit" value="关闭当前窗口" onclick="close3()">
    
  • 页面滚动

    			//页面滚动
    			function to_bottom(){
    				//window.scrollTo(x,y)
    				y = 0
    				y_max = 5000
    				while(y <= y_max){
    					y += 500
    					window.scrollTo(0,y)
    				}
    			}
    		<input type="submit" value="滚动到页面底部" onclick="to_bottom()">
    		
    		<div style="height:5000px;"></div>
    		<p>页面底部</p>
    
Day17 中,我们可以通过 Flask 框架快速搭建一个 BBS 论坛。具体步骤如下: 1. 创建 Flask 应用 ```python from flask import Flask app = Flask(__name__) ``` 2. 创建数据库 ```python from flask_sqlalchemy import SQLAlchemy app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///bbs.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) ``` 3. 创建数据库模型 ```python class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) password = db.Column(db.String(20), nullable=False) class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) ``` 4. 创建路由和视图函数 ```python @app.route('/') def index(): posts = Post.query.all() return render_template('index.html', posts=posts) @app.route('/post/<int:post_id>') def post(post_id): post = Post.query.get(post_id) return render_template('post.html', post=post) @app.route('/new_post', methods=['GET', 'POST']) def new_post(): if request.method == 'POST': title = request.form['title'] content = request.form['content'] user_id = 1 # 假设当前用户为 id 为 1 的用户 post = Post(title=title, content=content, user_id=user_id) db.session.add(post) db.session.commit() return redirect('/') return render_template('new_post.html') @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] user = User.query.filter_by(username=username, password=password).first() if user: session['user_id'] = user.id return redirect('/') else: flash('用户名或密码错误') return render_template('login.html') @app.route('/logout') def logout(): session.pop('user_id', None) return redirect('/') ``` 5. 创建 HTML 模板 创建 index.html、post.html、new_post.html、login.html 四个模板文件,并且使用 jinja2 模板引擎渲染数据。 6. 运行应用 ```python if __name__ == '__main__': app.run() ``` 以上就是快速搭建 BBS 论坛的主要步骤,当然在实际应用中还需要考虑更多细节问题,比如用户认证、数据校验、页面美化等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值