2 回答

TA貢獻1785條經驗 獲得超8個贊
創建一個函數,您可以使用要附加的文本進行調用p:
const container = document.querySelector('.wpcf7-response-output');
container.innerHTML = '';
const append = (text) => {
container.appendChild(document.createElement('p')).textContent = text;
};
append('Thank you for your order!');
append('A confirmation email has been sent to you from [email protected].');
append('Please keep the confirmation email for your records.');
或者只是分配新的 HTML 字符串:
document.querySelector('.wpcf7-response-output').innerHTML = `
<p>Thank you for your order!</p>
<p>A confirmation email has been sent to you from [email protected].</p>
<p>Please keep the confirmation email for your records.</p>
`;

TA貢獻1825條經驗 獲得超6個贊
干得好。您可以通過這種方式消除重復,同時保持函數的通用性。
泛化能力極大地有助于減少代碼。下面的appendChild函數不僅可以用于此父子組合,還可以用于任何其他父子組合。
'''
function appendChild(parentSelector,childTagname,innerhtml)
{
var childel = document.createElement(childTagname);
childel.innterHTML =innerhtml;
document.querySelector(parentSelector).appendChild(childel);
}
appendChild('.wpcf7-response-output','p',"Thank you for your order!");
appendChild('.wpcf7-response-output','p',"A confirmation email has been sent to you from [email protected].");
appendChild('.wpcf7-response-output','p',"Please keep the confirmation email for your records");
'''
添加回答
舉報