Jquery or Javascript 添加HTML對(duì)象

原始HTML

<div class="form-group">
    <label title="" class="control-label col-sm-4 col-xs-4">經(jīng)度</label>
    <div class="col-sm-8 col-xs-8">
         <input type="text" name="txt_T_Bas_Event.Longitude" id="txt_T_Bas_Event.Longitude" class="form-control" style="  " value="119"> 
    </div>
</div>
old.png

需要在文本框后添加坐標(biāo)拾取按鈕

實(shí)現(xiàn)效果

<div class="form-group">
    <label title="" class="control-label col-sm-4 col-xs-4">經(jīng)度</label>
    <div class="col-sm-8 col-xs-8"> 
        <div class="input-group">
                <input type="text" name="txt_T_Bas_Event.Longitude" id="txt_T_Bas_Event.Longitude" class="form-control" style="  " value="119">
                <span onclick="getLongitude();" class="input-group-addon" title="坐標(biāo)拾取">
                        <i class="fa fa-map-marker"></i>
                </span>
        </div>
    </div>
</div>
new.png

嘗試方法

使用 before、after,直接追加 HTML

            //添加經(jīng)緯度選擇框
            $("input[id$='.Longitude']").each(function () {
                //方法一
                $(this).before('<div class="input-group">');
                $(this).after('<span onclick="getLongitude();" class="input-group-addon" title="坐標(biāo)拾取"><i class="fa fa-map-marker"></i></span></div>')
            });

內(nèi)容加上了,可是按鈕換行,果斷不能忍


bugger.png

查看代碼如下

<div class="form-group">
    <label title="" class="control-label col-sm-4 col-xs-4">經(jīng)度</label>
    <div class="col-sm-8 col-xs-8"> 
        <div class="input-group"></div>
        <input type="text" name="txt_T_Bas_Event.Longitude" id="txt_T_Bas_Event.Longitude" class="form-control" style="  " value="119">
        <span onclick="getLongitude();" class="input-group-addon" title="坐標(biāo)拾取">
              <i class="fa fa-map-marker"></i>
        </span> 
    </div>
</div>

發(fā)現(xiàn) <div class="input-group"> 后自動(dòng)補(bǔ)全了 </div>

改進(jìn)方法一:Javascript outHTML

            //添加經(jīng)緯度選擇框
            $("input[id$='.Longitude']").each(function () {
                var this_id = $(this).attr("id");
                document.getElementById(this_id).outerHTML = '<div class="input-group">'
                    + document.getElementById(this_id).outerHTML
                    + '<span onclick="getLongitude();" class="input-group-addon" title="坐標(biāo)拾取"><i class="fa fa-map-marker"></i></span></div>';
            });

改進(jìn)方法二:Jquery prop outHTML

            //添加經(jīng)緯度選擇框
            $("input[id$='.Longitude']").each(function () {
                var oldHtml = $(this).prop("outerHTML");
                $(this).prop("outerHTML", '<div class="input-group">' + oldHtml + '<span onclick="getLongitude();" class="input-group-addon" title="坐標(biāo)拾取"><i class="fa fa-map-marker"></i></span></div>');
            });
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 原文鏈接 http://blog.poetries.top/2016/10/20/review-jQuery 關(guān)注...
    前端進(jìn)階之旅閱讀 16,679評(píng)論 18 503
  • 一、樣式篇 第1章 初識(shí)jQuery (1)環(huán)境搭建 進(jìn)入官方網(wǎng)站獲取最新的版本 http://jquery.co...
    凜0_0閱讀 3,451評(píng)論 0 44
  • 為甚嚒要學(xué)習(xí)jQuery? 因?yàn)镴S中有很多痛點(diǎn): window.onload事件只能出現(xiàn)一次,如果出現(xiàn)多次,后面...
    magic_pill閱讀 844評(píng)論 0 13
  • 1.永澄:打通的喜悅!我的自我管理系統(tǒng)升級(jí)簡(jiǎn)史 #觀察永澄50天-01天#生命中的每一次經(jīng)歷,都將成為未來(lái)人生的財(cái)...
    鬼谷老尤閱讀 281評(píng)論 0 0
  • 忍耐與寬容 大家早上好,歡迎打開(kāi)剽悍晨讀,每天進(jìn)步一點(diǎn)點(diǎn),堅(jiān)持帶來(lái)大改變。在互聯(lián)網(wǎng)時(shí)代,我們每個(gè)人都想讓自己變的更...
    東方踏痕Q閱讀 421評(píng)論 0 1