我想將h4標籤中的文本更改爲其祖父div的類名(即sds 2016 gwap)。jquery selector在div下選擇h4標籤
<div class="grid">
<div class="office-year-function sds 2016 gwap">
<div class="content cell-child">
<img src="535/img/01.jpg" alt="" width="312px" height="160px">
</div>
<div class="img-hover cell-child">
<br/>
<h3>GWAP Kick-off Ceremony</h3>
<h3>2016</h3>
<h4></h4>
當文檔加載完成後,它會運行下面的函數。
function changeTag() {
for (var i = 1; i <= $('.office-year-function').length; i++) {
$this = $('div.grid div.office-year-function:nth-child(' + i + ')');
var className = $this.attr('class');
className = className.split(" ");
for (var j = 1; j < className.length; j++) {
className[j] = className[j].replace("-", " ");
if (className[j] == "gwap") {
$this.find("h4").html += className[j].toUpperCase();
} else {
$this.find('h4').html += className[i].capitalize();
}
}
}
}
但
$this.find("h4").html+=className[j].toUpperCase();
$this.find('h4').html+=className[i].capitalize();
這有什麼錯我的代碼有錯誤?
'html()'是一個函數,你被視爲屬性。使用它像'$ this.find(「h4」)。append(className [j] .toUpperCase());' – Satpal
'.html'是一個函數。將它用作'.html(content + classname)' –