【问题标题】:Jquery.ui : Get id from current elementJquery.ui : 从当前元素获取 id
【发布时间】:2012-12-29 16:32:45
【问题描述】:

在调整大小时如何获取当前元素的 id?我做的以下不起作用,谢谢

<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(document).ready(
function(){
    $( ".resizable" ).resizable({
        resize: function( event, ui ) {alert(ui.element.id)}
    })
}
);
</script>

<img class="resizable" id="25" src="test1.jpg">
<img class="resizable" id="26" src="test2.jpg">
<img class="resizable" id="27" src="test3.jpg">

【问题讨论】:

  • 我不确定这是否被提及,但在 HTML ID 中不能以数字开头。

标签: jquery resize resizable


【解决方案1】:

试试这个

$( ".resizable" ).resizable({
    resize: function( event, ui ) {
            alert($(ui.element).children('img').attr('id'));
    }
});

【讨论】:

  • @HermannGolden 经过一些测试,我发现它添加了一个包装器 div,因此您需要使用 $(ui.element).children('img').attr('id')。我已更新我的答案以显示这一点
【解决方案2】:

使用$(this)。它对我有用。

【讨论】:

    猜你喜欢
    • 2017-04-01
    • 2013-09-10
    • 2017-10-12
    • 2012-08-04
    • 2012-07-05
    • 2016-03-31
    • 1970-01-01
    • 2016-11-18
    • 2013-03-01
    相关资源
    最近更新 更多