我正在嘗試編寫一個簡單的腳本,它將更改頁面上一些錨點的文本。我對JavaScript很新,我可以更改錨點,但它會更改整個標記,包括刪除href。只更改錨點的文本javascript
如何僅編輯文本而不影響href?
<body>
<div class="loop-add-to-cart">
<a href="/?preview_id=10822&preview_nonce=2f9a98329f&_thumbnail_id=-1&preview=true&add-to-cart=11544" rel="nofollow" data-product_id="11544" data-product_sku="" data-quantity="1" class="add_to_cart_button product_type_simple ajax_add_to_cart">Add to basket</a>
<div class="wpd-buttons-wrap-simple" data-id="11544">
<a href="http://802.f5f.myftpupload.com/11550-2/design/11544/" class="mg-top-10 wpc-customize-product">Design from blank</a>
</div>
</div>
<script>
function buybuttons() {
var buybuttons = document.getElementsByClassName('wpd-buttons-wrap-simple');
for(var i = 0; i < buybuttons.length; i++){
buybuttons[i].innerHTML="Test";
};
}
buybuttons();
</script>
</body>
你能發表一個你如何抓錨的例子嗎?看看innerHTML - https://www.w3schools.com/jsref/prop_html_innerhtml.asp – Rastalamm
你正在改變div的'innerHTML',而不是'a'。使用'document.querySelectorAll(「。wpd-buttons-wrap-simple a」)':https://jsfiddle.net/khrismuc/rLrpv0n1/ –
謝謝你的工作! –