我想使用Javascript在类中追加文本,而不是使用jQuery。 示例:
class BaseClass {
class func update() {
}
}
class Subclass1: BaseClass {
override class func update() {
print("Subclass1")
}
}
class Subclass2: BaseClass {
override class func update() {
print("Subclass2")
}
}
class Subclass3: BaseClass {
override class func update() {
print("Subclass3")
}
}
let allSubclasses: [BaseClass.Type] = [
Subclass1.self,
Subclass2.self,
Subclass3.self
]
for i in 0 ..< allSubclasses.count {
allSubclasses[i].update()
}
将文字放入<table>
<tr>
<th>Name</th>
<td>John Vicket</td>
<tr>
<tr>
<th>Email</th>
<td>email@gmail.com</td>
<tr>
<tr>
<th>Telephone</th>
<td>425 4657 451</td>
<tr>
<tr>
<th>Address</th>
<td>1st Ave, Nyc</td>
<tr>
<table>
后,它应如下所示:
<div class="A">
<div class ="B">
<div class="C">
</div>
</div>
<div>
我想在不使用jQuery的情况下这样做。
答案 0 :(得分:0)
使用insertAdjacentHTML()
方法:
var elements = document.getElementsByClassName("B"); // get all elements with class 'B'
for (var i = 0; i < elements.length; i++) { // for every element with class 'B':
elements[i].insertAdjacentHTML("afterbegin", "This is text");
// insert text just inside the element
}
<div class="A">
<div class="B"><!-- Text will be inserted here -->
<div class="C">
</div>
</div>
<div>
答案 1 :(得分:0)
这也可以解决问题:
function prependTextToClassB(){
var textNode = document.createTextNode('This is text');
var elB = document.querySelector('.B');
var elC = document.querySelector('.C');
elB.insertBefore(textNode, elC);
}