假设我有这段代码:
<div id="Element_1" class="draggable">
<input type="text" placeholder="Enter Your Text Here" style="width:300px;">
</div>
<div id="Element_2" class="draggable">
<textarea placeholder="Enter Your Text Here" style="width:400px;height:200px;"></textarea>
</div>
我要做的是从“div”的子项和“Type”(输入/标记名)中获取元素属性值,以便将值存储在变量中。
目前我可以在Var中获取元素和存储,这是我的代码:
$(".draggable").live("click",function(){
var SelectedID = $(this).attr("id");
$("*").removeClass("selected");
var Element = $("#"+SelectedID,":first-child").addClass("selected").html();
PlaceholderPropValue = $(element).attr("placeholder");
StylesPropValue = $(element).attr("style");
WidthProp = StylesProp.match(/(width:)([0-9].*?)(?=px;)/)[2];
HeightProp = StylesProp.match(/(height:)([0-9].*?)(?=px;)/)[2];
alert(PlaceholderPropValue+ " " +WidthProp+ " " +HeightProp);
});
谢谢!
卡尔
答案 0 :(得分:0)
你的代码有点矫枉过正。
$('.draggable').click(function(){ var $element = $(this).children(); var width = parseInt($element.width()); var height = parseInt($element.height()); var placeholder = $element.attr('placeholder'); alert(placeholder+ " " +width+ " " +height); })