【问题标题】:how to transmit styles from one element to another?如何将样式从一个元素传输到另一个元素?
【发布时间】:2013-10-09 16:44:57
【问题描述】:

如何为一个元素设置样式并将其放入另一个元素中?我的目标是摆脱包装器和 .ui-resizable-handlers,只留下 img,但用替换的样式形成包装器。 有可能吗?

<div class="ui-wrapper ui-draggable" style="overflow: hidden; position: absolute; width:  492px; height: 411px; top: 105px; left: 409px; margin: 0px;">

    <img id="link1" style="position: static; margin: 0px; resize: none; zoom: 1; display: block; height: 411px; width: 492px;" src="http://i.imgur.com/gKY7j0l.jpg" class="ui-resizable">

    <div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;">
    </div><div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;">
    </div><div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;">
    </div>
</div>

因此在本例中,结果将是带有包装器样式的 img:

<img id="link1" style="overflow: hidden; position: absolute; width:  492px; height: 411px; top: 105px; left: 409px; margin: 0px;" src="http://i.imgur.com/gKY7j0l.jpg" class="ui-resizable">

【问题讨论】:

  • 您自己尝试过吗?

标签: javascript jquery dom-manipulation


【解决方案1】:

我是 JQuery 的新手,只有几个月的经验。 不过,我认为这应该可行。

var hold = $('div.ui-wrapper.ui-draggable').attr('style');
$('#link1').attr('style',hold);

然后您可以移除包装或做任何您必须做的事情。 要从包装器中解包,http://api.jquery.com/unwrap/ 应该很有用。

【讨论】:

    【解决方案2】:

    你应该把风格融入课堂。例如:

    <div class="ui-wrapper ui-draggable" style="overflow: hidden; position: absolute; width:  492px; height: 411px; top: 105px; left: 409px; margin: 0px;">
    
    <img id="link1" style="overflow: hidden; position: absolute; width:  492px; height: 411px; top: 105px; left: 409px; margin: 0px;" src="http://i.imgur.com/gKY7j0l.jpg" class="ui-resizable">
    

    <div class="ui-wrapper ui-draggable WRAP">
    <img id="link1" class="ui-resizable WRAPIMG">
    

    那么你可以这样使用:

    $('#link1').unwrap().removeClass('WRAPIMG').addClass('WRAP');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-28
      • 2012-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 2010-11-12
      相关资源
      最近更新 更多