"。setAttribute(("等等。我使用了一些价值观,但没有成功。
href>数据href
默认情况下有一个href元素
"<a class="wp-block-post-excerpt__more-link" href="#link" tabindex="0">View Details</a>"
我想使用js将此元素更改为datahref。此外,我不想再添加一个。
"<a class="wp-block-post-excerpt__more-link" data-href="#link" tabindex="0">View Details</a>"
我正在使用以下js代码,href和data href都已添加,我想将其作为链接删除。
$('a[href]').each(function() {
$(this).attr('data-href', $(this).attr('href'));
});
简单使用香草Javascript:
- 获取
href
属性,将其设置在变量oldHref
中 - 使用removeAttribute删除
href
- 使用setAttribute添加该
oldHref
变量的新数据属性
let link = document.querySelector('.wp-block-post-excerpt__more-link');
let oldHref = link.href;
link.removeAttribute('href');
link.setAttribute("data-href", oldHref);
<a class="wp-block-post-excerpt__more-link" href="#link" tabindex="0">View Details</a>