By GokiSoft.com| 11:05 13/06/2020|
Học JS

[Share Code] Phương trình bậc nhất ax+b=0 javascript - giải phương trình bậc nhất ax+b=0 - a*x+b=0 javascript - document.getElementById + innerHTML

Viết chương trình sau

Lời giải


#tags-in-javascript.html


<!DOCTYPE html>
<html>
<head>
	<title>Events in Javascript</title>

	<style type="text/css">
		.panel {
			display: block;
		}

		.panel .panel-heading {
			background-color: #4287f5;
			height: 30px !important;
			color: white;
			font-size: 20px;
			padding: 10px;
		}

		.panel .panel-body {
			background-color: grey;
			padding: 20px;
		}

		button {
			background-color: #f27f55;
			color: white;
			font-size: 18px;
		}

		input {
			font-size: 18px;
		}
	</style>
</head>
<body onload="init()">
	<div class="panel">
		<div class="panel-heading">
			Giai phuong trinh bac nhat ax+b=0
		</div>	
		<div class="panel-body">
			<input type="number" id="a" placeholder="Nhap A" step="0.01" onchange="changeA()">
			<input type="number" id="b" placeholder="Nhap B" step="0.01">
			<button onclick="printResult()">Hien Thi Ket Qua</button>
			<p style="color:white; font-size: 20px" id="result">
			</p>
		</div>
	</div>

	<script type="text/javascript">
		function changeA() {
			// test su kien onchange.
			var aTag = document.getElementById('a')
			console.log(aTag.value)
		}

		function printResult() {
			console.log('hien thi ket qua')
			//lay tag thong qua id
			var aTag = document.getElementById('a')
			var bTag = document.getElementById('b')

			var resultTag = document.getElementById('result')

			var a = aTag.value
			var b = bTag.value

			var str = a+'*x + ' + b + ' = 0<br/>'

			if(a == 0) {
				if(b == 0) {
					console.log('PTVSN')
					str += 'PTVSN'
				} else {
					str += 'PTVN'
				}
			} else {
				x = -b/a;
				console.log('x = ' + x)
				str += 'Nghiem PT: '+x
			}
			resultTag.innerHTML += '<br/>'+str;
		}

		function init() {
			//bat dau xu ly code => khi load trang.
			console.log('init...')
		}
	</script>
</body>
</html>


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)