我在解析HTML之前需要打開一些頁面上的常見問題,因爲答案的文本值是隱藏的,因此在解析HTML時會出現空白的頁面。將頁面上的所有aria擴展爲false使用javascript
當我在瀏覽器中使用下面的代碼時,它會返回所有鏈接,但它不會將「aria-expanded」屬性更改爲「false」。
$('a').each(function() { this.setAttribute('aria-expanded', 'false')});
我在解析HTML之前需要打開一些頁面上的常見問題,因爲答案的文本值是隱藏的,因此在解析HTML時會出現空白的頁面。將頁面上的所有aria擴展爲false使用javascript
當我在瀏覽器中使用下面的代碼時,它會返回所有鏈接,但它不會將「aria-expanded」屬性更改爲「false」。
$('a').each(function() { this.setAttribute('aria-expanded', 'false')});
jQuery的方式應該工作:
$(function() {
$('a').each(function() {
$(this).attr('aria-expanded', 'false')
});
});
我不相信this
指物體你想要(一個標籤)。你可以嘗試下面的代碼。
$('a').each(function(index, obj) { $(obj).attr('aria-expanded', 'false') });
這使我返回與我的代碼相同的對象。 – kellykels21
setAttribute是原生JavaScript函數。使用jquery的attr()。 ('a')。each(function(){this.attr('aria-expanded','false')});
由於某些原因,Firefox說this.attr()不是函數? – kellykels21
這不是在當前範圍內定義的。每個函數回調都將參數作爲索引和當前元素。 ('a')。each(function(index,element){$(element).attr('aria-expanded','false')}); –
我會嘗試用.attr()(http://api.jquery.com/attr/)
this.attr( '詠歎調擴展', '假')}
這裏是我回來後,運行你的代碼:$(function(){$('a')。each(function(){$(this).attr('aria-expanded','false')});}); 對象{context:HTMLDocument→newsite,長度:1,多1 ...} 'mozHidden'和'mozVisibilityState'已棄用。請使用前綴「hidden」和「visibilityState」代替。 – kellykels21
@ kellykels21你可以用你的瀏覽器名稱和版本以及jQuery版本更新你的poste嗎? – smarber