저는 JavaScript에서 꽤 새로워졌습니다. 생성자의 버튼이 내가 만든 테이블에 나타나지 않는 이유를 꽤 잘 모르겠습니다. 내가 생성자에 버튼을 만들거나 할 수 있다면 사람이 내가 동적으로자바 생성자에서 버튼 만들기
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html">
<title>Client Side Shopping Basket</title>
<meta name="author" content="Justin Butterworth">
<link href="basket.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="container">
<header id="title">
<h1>Products</h1>
</header>
<div id="output">
<table border="1" id="tProducts">
<tr>
<th>Name</th>
<th>Description</th>
<th>Quantity</th>
<th>Price</th>
<th>Gender</th>
</tr>
</table>
</div>
<br /><br />
<header id="title">
<h1>Shopping Basket</h1>
</header>
<div id="totals">
<table border="1">
<tbody>
<!-- tax + subtotal -->
<tr class="netcost">
<td class="light">Net Total:</td>
<td colspan="2" class="light"></td>
<td> </td>
<td> </td>
</tr>
<tr class="totalcost">
<td class="light">Total:</td>
<td colspan="2"> </td>
<td colspan="2"><span class="thick">£225.45</span></td>
</tr>
<!-- checkout btn -->
<tr class="checkoutrow">
<td colspan="5" class="checkout"><button id="submitbtn">Checkout Now!</button></td>
</tr>
</tbody>
</table>
</div>
</div>
<p id="test"></p>
<script>
var productList=[]; //where product objects are to be held
var basket=[];
//constructor
function Products(name, description, quantity, price, gender)
{
var obj = this; // a reference to this object
this.name = name;
this.description = description;
this.quantity = quantity;
this.price = price.toFixed(2);
this.gender = gender;
this.getName = function() {
return this.name
};
this.getPrice = function() {
return '\u00A3' + this.price;
};
}
//instantiate new products
var shorts = new Products('Shorts', 'Stone Wash Demin Shorts', 20, 25.90, 'F');
var bag = new Products('Bag', 'Leather Shoulder Bag', 4, 50.45, 'F');
var blouse = new Products('Blouse', 'Vintage Blue Silk Polka Dot Blouse', 8, 45.99, 'F');
var boots = new Products('Boots', 'Soft Leather Brown Ankle Boots', 3, 65.35, 'F');
var belts = new Products('Belts', 'Woven Finish Fashion Belt', 15, 21.99, 'F');
var shirt = new Products('Shirt', 'Jacquard Pattern Wrangler Western Shirt', 19, 34.87, 'M');
var shoes = new Products('Shoes', 'Suede Ankle Boots', 6, 55.00, 'M');
var trousers = new Products('Trousers', 'Izod Peach Chinos', 23, 31.75, 'M');
var belt = new Products('Belt', 'Suede Casual Belt', 4, 22.98, 'M');
var hat = new Products('Hat', 'Trilby Style Brown Woven Fix', 2, 67.80, 'M');
//add objects to an array
productList.push(shorts, bag, blouse, boots, belts, shirt, shoes, trousers, belt, hat);
//function to display full product list
function displayProducts(tProducts)
{
var table = document.getElementById('tProducts'); // reference to the table to add rows to
for (var i = 0; i < tProducts.length; ++i) // iterate through the array for each of the products
{
var product = tProducts[i]; // keep a reference to each individual product
var row = document.createElement('tr'); // create a row element to append cells to
var properties = ['name', 'description', 'quantity', 'price', 'gender']; // properties of the array elements
for (var j = 0; j < properties.length; ++j) // append each one of them to the row in question, in order
{
var cell = document.createElement('td'); // create new data cell for names
cell.innerHTML = product[properties[j]]; // set name of property using bracket notation (properties[j] is a string, which can be used to access properties of product)
row.appendChild(cell); // add to end of the row
}
row.appendChild(createBtn());
table.appendChild(row); // add new row to table
}
};
function createBtn() {
var btn = document.createElement('input');
btn.type = 'button'
btn.name = name;
btn.value = 'Add';
btn.onclick = function() {
return obj.getPrice();
};
return btn;
};
displayProducts(productList);
</script>
</body>
</html>
좋아요 버튼을 생성하기 위해 별도의 기능을 필요로 말해 줄 수 .. 나는 버튼 요소에 자신이 만든하지만 어떻게 난의 이름을 업데이트 할 수 있습니다 제품 이름 단추? 자바 스크립트에서 머리 글자를 얻으려고하는 한 가지 측면 인 범위 문제입니다.
var p = new products("name", "des", 1, 1, "");
p.createBtn();
하고 나를 위해 잘 작동 :
@RobG 또한 내가 당신의 기능을 시도 모든 코드
어떻게'.createBtn()'함수가 호출되고 있습니까? 그리고'document.write()'는 아마도 잘못된 접근법 일 것입니다. – Pointy
load 이벤트가 전달 된 후 * instance.createBtn *을 호출하는 경우 * document.write *는 모든 내용의 문서 전체를 지우고 단일 단추를 삽입합니다. – RobG
* createBtn * 함수는 DOM 메서드를 사용하여 버튼을 만들고 새로 만든 버튼을 반환하여 문서의 올바른 위치에 추가하거나 버튼이 될 인수로 DOM 요소를 가져와야합니다 에 추가됨. 오, 그리고 생성자 안에서 이것을하는 것은 말이되지 않습니다. * this.getPrice *는 * products * 인스턴스가 아닌 버튼의 * getPrice * 속성을 참조합니다. – RobG