如何在JavaScript/jQuery中按鈕點擊動態添加div? 我想要div的所有格式都有「listing listing_ad job」類。在JavaScript/jQuery中使用按鈕點擊動態添加div
這是我試過的代碼,使用jQuery。
$('#btnAddtoList').click(function(){
\t var newDiv = $('<div class="listing listing_ad job"><h4><a>Some text</a></h4> </div>');
//newDiv.style.background = "#000";
document.body.appendChild(newDiv);
});
.listing {
border-bottom: 1px solid #ddd;
float: left;
padding: 0 0 5px;
position: relative;
width: 559px;
}
.listing:hover {
background: #f5f5f5 none repeat scroll 0 0;
border-bottom: 1px solid #ddd;
cursor: wait;
}
a:hover {
color: #ff5050;
}
.subtitle {
width: 430px;
font-weight: normal;
font-size: 12px;
font-family: Arial;
color: #7f7f7f;
}
.info {
float: left;
margin: 10px 15px 5px;
min-width: 500px;
clear: both;
color: #7f7f7f;
margin: 15px 44px 15px 0;
overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnAddtoList">
Add to list
</button>
<div class="listing listing_ad job">
<h4>
<a>
Excellent Opportunity For Internship at Diamond
</a>
</h4>
<div class="subtitle">
Lahore, Punjab
</div>
<div class="info">
This is Info/Description.
</div>
</div>
<!-- ************************ -->
<div class="listing listing_ad job">
<h4>
<!-- Src: http://jobs.mitula.pk/internship-program-lahore-jobs -->
<a>
Excellent Opportunity For Internship at Diamond
</a>
</h4>
<div class="subtitle">
Lahore, Punjab
</div>
<div class="info">
This is Info/Description.
</div>
</div>
東西像什麼? – 2pha
@ 2pha ...我有難以發佈的問題。 – Asad