我如何在课堂上获得属性

时间:2019-05-02 02:04:40

标签: jquery html

我想在课堂上获得属性,但这不起作用

我使用Django,html,php,查询

root

所以我做到这一点

    <div id="yui_patched_v3_18_1_1_1556713475044_913" 
class="diagram-node-task yui3-widget yui3-overlay diagram-node yui3-widget-positioned yui3-widget-stacked"  
tabindex="1" data-nodeid="diagramNode_field_task923"  
style="height: 70px; width: 70px; left: 441px; top: 161px; z-index: 100;">

还有这个

$(".diagram-node-task yui3-widget yui3-overlay diagram-node yui3-widget-positioned yui3-widget-stacked").attr('style')

但是它们不起作用(它们向我显示未定义)

我想在此代码中获得宽度,高度之类的样式

2 个答案:

答案 0 :(得分:0)

更改为此

let style = $(".diagram-node-task.yui3-widget.yui3-overlay.diagram-node.yui3-widget-positioned.yui3-widget-stacked").attr('style');
console.log(style);

因为所有类都在同一个div中,所以您需要将查询选择器添加为.diagram-node-task.yui3-widget.yui3-overlay.diagram-node.yui3-widget-positioned.yui3-widget-stacked

let style = $(".diagram-node-task.yui3-widget.yui3-overlay.diagram-node.yui3-widget-positioned.yui3-widget-stacked").attr('style');
console.log(style);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="yui_patched_v3_18_1_1_1556713475044_913" 
class="diagram-node-task yui3-widget yui3-overlay diagram-node yui3-widget-positioned yui3-widget-stacked"  
tabindex="1" data-nodeid="diagramNode_field_task923"  
style="height: 70px; width: 70px; left: 441px; top: 161px; z-index: 100;">

答案 1 :(得分:0)

要选择特定的style,请使用css()。您只能使用一个类来获取属性。希望这会有所帮助

var style = $(".yui3-widget-stacked").attr('style')
var width= $(".yui3-widget-stacked").css('width')

console.log("Style: "+style)
console.log("Width: "+width)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div id="yui_patched_v3_18_1_1_1556713475044_913" 
class="diagram-node-task yui3-widget yui3-overlay diagram-node yui3-widget-positioned yui3-widget-stacked"  
tabindex="1" data-nodeid="diagramNode_field_task923"  
style="height: 70px; width: 70px; left: 441px; top: 161px; z-index: 100;">