如果子DIV包含特定的CLASS和STYLE属性,则指定DIV ID
Assign a DIV ID if a Child DIV contains a certain CLASS and STYLE attribute
我有一个使用CMS的网站,我需要保留实际的页面内容并剥离所有标题&页脚代码,但是没有DIV ID标签,我没有访问后端来添加ID。
我的问题是:
如何添加一个DIV ID 'content'到' DIV style="display:inline;float:left;"‘如果其子DIV包含'class="myclass"' AND '样式。width="610px"'使用javascript或jQuery?
谢谢!
这是我今天的代码:
<div>
<div style="display:inline;float:left;">
<div class="myclass" style="clear:both;width:610px;display:inline;float:left;">Content A</div>
<div class="myclass" style="clear:both;width:610px;display:inline;float:left;">Content B</div>
</div>
</div>
我想把它改成:
<div>
<div id="content" style="display:inline;float:left;">
<div class="myclass" style="clear:both;width:610px;display:inline;float:left;">Content A</div>
<div class="myclass" style="clear:both;width:610px;display:inline;float:left;">Content B</div>
</div>
</div>
试试这个:
var div$ = $('[style="display:inline;float:left;"]');
var childDiv$ = $('[class="myclass"][style*="width:610px;"]')
if(div$.children(childDiv$).length){
div$.attr('id','content');
}
如果您确定属性用于样式化,则使用该属性进行搜索。
否则,您需要根据类名进行选择并过滤掉不需要的。请记住,float
'ed元素不能是display: inline;
,它总是display: block;
。
$(function(){
$(".myclass").each(function() {
var that = $(this);
var parent = that.parent();
if(that.css("width") === "610px" && parent.css("display") == "block" && parent.css("float") == "left") {
parent.attr("id", "content");
}
});
});
相关文章:
- 使用类属性切换Div值
- 使用 Div 标签的 location.href 属性时,Eval 不起作用
- 将必需属性添加到DIV On单选按钮更改中的输入
- 切换一个Div与按钮改变左边距属性
- 添加/删除 CSS 类属性以尊重不基于 Div id 的类
- 使用 Javascript 从 Div 中检索 href 属性(链接)
- 从我的 Div 背景图像属性中循环 3 个图像
- 垂直标签:删除没有jQuery或固定位置属性的UL和DIV中断
- 按内联“样式”属性对 Div 进行排序
- 定位 AJAX 会通过使用“rel”属性生成正确的 DIV
- 按属性对Div进行排序-asc或desc
- JavaScript基本问题:针对DOM中的特定Div并更改属性
- 更改类或CSS”;显示“;DIV的属性,并平滑地显示它
- 绑定在knockoutjs中拖动DIV元素属性到对象属性
- jQuery用动态ID和定义的CSS属性创建DIV(定义该DIV的外观和位置)
- 显示/隐藏基于数据属性的Div
- Div类搜索使用数据过滤属性
- 改变在IE 11中不工作的DIV的旋转速度,使用JS来更新CSS动画属性
- 动态添加的Div不会拾取CSS属性
- 在DIV中显示图像的标题属性