2015-07-21 131 views
3

如何在作为对象给定的某些HTML代码中更改标记的属性?将属性添加到对象

目前,如果我做的:

console.log(gallery.currItem); 

我得到:

所以我当时做的事:

console.log(gallery.currItem.html); 

我也得到控制台的HTML(不作为一个对象,我相信它就像texy一样):

<video width="500" height="250" controls><source src="" type=""></video> 

但我不知道如何通过添加属性muted="muted"来编辑video标记。

我想:

console.log($(gallery.currItem.html).find('video')); 

但这又返回的对象。 :/

+2

如何gallery.currItem初始化/设置? – depperm

+0

即使它看起来像控制台中的文本,它也是实际的元素。 –

+0

@塞巴斯蒂安:不,它不是。看截图。 –

回答

0

试试这个。

$(gallery.currItem.html).attr("muted", "muted"); 
+0

这可能会从HTML中产生一个新元素,并根据需要对其进行更改,但不会将其分配回库中。 – Jacob

+0

没有工作。 :( – user5140488

+0

@Jacob:是的,你说得对 – jrath

1

我假设你使用的是PhotoSwipe。 gallery.currItem.html不是一个字符串,而是一个实际的html元素。 您只需直接编辑它的属性:

gallery.currItem.html.setAttribute("muted", "muted"); 

,以确保它是一个实际的元素,如果有问题,这样做:

if(gallery.currItem.html.tagname) { 
    gallery.currItem.html.setAttribute("muted", "muted"); 
} else { 
    // append it to the dom or wrap it into jquery and then set the attributes 
    gallery.currItem.html = $(gallery.currItem.html).attr("muted", "muted")[0]; 
} 
+0

TypeError:gallery.currItem.html.setAttribute不是函数 – user5140488

+0

好吧,然后你必须包装它在jquery中。这将是相同的其他答案已经建议你可以用jquery对象覆盖html属性。 –

0

答:

$(gallery.currItem.container.lastChild).attr('muted', 'muted');