[英]Add class to parent div
I am working with the google maps drawing manager.我正在与谷歌地图绘图管理器合作。 They don't put id's or class names on the drawing tools button bar so I'm trying to do this myself.他们不会在绘图工具按钮栏上放置 id 或类名称,所以我自己尝试这样做。
First I want to remove the circle button which the below works fine, but I want to add my own button so need to add a class name to the parent div "gmnoprint" but google has about 5 div's all with the same class name.首先,我想删除下面可以正常工作的圆形按钮,但我想添加我自己的按钮,因此需要向父 div“gmnoprint”添加一个类名,但谷歌有大约 5 个 div 都具有相同的类名。 I just want to add it to the one where the circle button was found.我只想将它添加到找到圆形按钮的那个。
<div class=gmnoprint"></div>
<div class=gmnoprint"></div>
<div class=gmnoprint"></div>
<div class=gmnoprint">
<div>
<div> <== This is what I found in my search
<span>
<div>
<img></img>
</div>
</span>
</div>
</div>
</div>
I am able to find the element I want and remove it, but adding a class to its wrapper div is proving a bit difficult for me.我能够找到我想要的元素并将其删除,但是向其包装器 div 添加一个类对我来说有点困难。
This works for removing the button这适用于删除按钮
$(".gmnoprint").each(function(){
$(this).find("[title='Draw a circle']").remove();
});
This doesn't work.. Just add's the class to all ".gmnoprint" div's这不起作用..只需将类添加到所有“.gmnoprint”div
$(".gmnoprint").each(function(){
$(this).find("[title='Draw a circle']").remove().parent().addClass("test");
});
remove()
removes the element from the DOM and returns the free-standing jquery object which has no connection to the DOM at all. remove()
从 DOM 中删除元素并返回与 DOM 完全没有任何连接的独立 jquery 对象。 A call to parent()
after calling remove()
is incorrect and that likely is the cause for your issue.在调用remove()
parent()
之后调用parent()
是不正确的,这可能是导致您出现问题的原因。
Try splitting your statements to:尝试将您的语句拆分为:
var toRemove = $(this).find("[title='Draw a circle']");
toRemove.parent().addClass("test");
toRemove.remove();
You can use jQuery insertAfter
and out your button after that default button then remove it.您可以在该默认按钮之后使用 jQuery insertAfter
并删除您的按钮,然后将其删除。
$(".gmnoprint").each(function(){
var defBtn = $(this).find("[title='Draw a circle']");
$('<button class="my-button" />').insertAfter(defBtn);
defBtn.remove();
});
Or use jQuery after
like this:或使用jQuery after
是这样的:
$(".gmnoprint").each(function(){
$(this)
.find("[title='Draw a circle']")
.after($('<button class="my-button" />'))
.remove();
});
You can use child selector
to target the elements您可以使用child selector
来定位元素
$(".gmnoprint > div > div").addClass('myClassName');
At that point you could replace the html of the whole div , or find the span and replace it's inner html.那时你可以替换整个 div 的 html ,或者找到 span 并替换它的内部 html 。 Using html()
method you don't need to use remove()
as it will replace all contents of the element(s)使用html()
方法您不需要使用remove()
因为它将替换元素的所有内容
$(".gmnoprint > div > div").addClass('myClassName').find('span').html('<newButton>');
API Reference : http://api.jquery.com/child-selector/ API 参考: http : //api.jquery.com/child-selector/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.