简体   繁体   中英

How to wrap an element inside a div?

I would like to wrap the image element inside a div element in Polymer 1.0:

<template>
      <img src="" alt="Image preview..." id="avatar" class="avatar">
    </template>

<script>
  var $container = document.createElement('div');
  $container.className += " " + 'resize-container';

  var image_target = this.$.avatar;
  var image_target_parent = image_target.parentNode;

  Polymer.dom(image_target_parent).insertBefore($container, image_target);
</script>

I don't know how to wrap the this.$.avatar inside $container .

I'm looking for a jQuery like solution as follows:

image_target.wrap('<div class="resize-container"></div>');

As explained here , you should use:

Polymer.dom($container).appendChild(this.$.avatar);

Also your 2nd line, it should be:

$container.className = 'resize-container'

Otherwise you are setting the class to .resize-container which isn't right since you do not use periods in class names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM