简体   繁体   中英

Place span before anchor tag text jQuery

I'm trying to make a button like this using jQuery/JS

所需的按钮图像

This button is generated using server side rails helper.While inspecting this element I got this

屏幕截图

For this I wrote the following code

   var editLink = makeElement('a', {
            class: 'edit-merchant-url btn btn-primary',
            href: url.BASE_MERCHANT_URLS + data.id + url.EDIT,
            innerHTML: 'Edit'
        });

        var editInnerSpan = makeElement('span', {class: 'fa fa-pencil-square-o'});

        editLink.appendChild(editInnerSpan);


 /**
         * It creates the dynamic DOM element
         * element should be tag name such as div, input, form
         * options should be attributes including class, id, innerHTML
         */

        function makeElement(element, options) {
            var $formField = document.createElement(element);
            $.each(options, function (key, value) {
                if (key === 'innerHTML') {
                    $formField.innerHTML = value;
                }
                else {
                    $formField.setAttribute(key, value);
                }
            });
            return $formField;
        }

But this create button like

输出

and while inspecting this I got this.

输出检查

I don't know what I'm doing wrong.

应该:

editLink.prependChild(editInnerSpan)

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