我想創建一個腳本來動態創建錨標記和鏈接到一個特定的元素。但是,當我輸出錨文本時,它會在散列和文本之間添加一堆空格。jQuery以某種方式添加空格輸出
$(document).ready(function(){
$('.accordion h2').each(function(){
var thisText = $(this).text();
var anchorText = thisText.replace(/ /g, "-");
var anchorLink = '<a name="' + anchorText + '"></a>';
var anchorTextFull = '<a href="#' + anchorText + '">' + thisText + '</a>';
$(this).before(anchorLink);
$(this).after(anchorTextFull);
});
});
此代碼輸出anchorTextFull變量如下:
<a href="# foo-bar">Foo Bar</a>
在哪裏這些空間是從哪裏來的?
感謝
精美的合作,謝謝! – 2011-05-16 17:52:39