Attributes 从没有ID的子元素获取属性值-JQuery
假设我有以下代码:Attributes 从没有ID的子元素获取属性值-JQuery,attributes,children,Attributes,Children,假设我有以下代码: <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="
<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>
谢谢
卡尔你的代码有点过分了
- 方法通常返回元素的子元素集,但在您的示例中只有一个,所以它是可以的
- .width()和.height()返回类似“300px”的值,因此我们使用parseInt()生成int值
- .click(handler)是.on(“click”,handler)的快捷方式
$(".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);
});
$('.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);
})