最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】jquery中如何插入html元素

HTML crifan 2759浏览 0评论

折腾:

【已解决】用Bootstrap给html中添加显示后可以消失掉的提示或警告

期间,需要用jquery去插入一个html元素:

        <div id="audio_play_prevented" class="alert alert-warning alert-dismissible col-md-12 col-xs-12">

          <button type = "button" class="close" data-dismiss = "alert">x</button>

          <strong>Notice:</strong> Auto play prevented, please mannually click above play button to play

        </div>

jquery insert html

.append() | jQuery API Documentation

.html() | jQuery API Documentation

jQuery – 添加元素

Insert HTML with jQuery – Stack Overflow

结果写成带换行的话:

        var audioPlayPreventedNoticeHtml = ‘        <div id="audio_play_prevented" class="alert alert-warning alert-dismissible col-md-12 col-xs-12">

        <button type = "button" class="close" data-dismiss = "alert">x</button>

        <strong>Notice:</strong> Auto play prevented, please mannually click above play button to play

        </div>’;

        console.log("audioPlayPreventedNoticeHtml=%o", audioPlayPreventedNoticeHtml);

会报错:

去掉换行试试

    function showAudioPlayPreventedNotice(){

        console.log("showAudioPlayPreventedNotice");

        // var prevDisplayValue = $("#audio_play_prevented").css("display");

        // console.log("prevDisplayValue=%o", prevDisplayValue);

        // $("#audio_play_prevented").css({"display":"block"});

        var audioPlayPreventedNoticeHtml = ‘<div id="audio_play_prevented" class="alert alert-warning alert-dismissible col-md-12 col-xs-12"><button type = "button" class="close" data-dismiss = "alert">x</button><strong>Notice:</strong> Auto play prevented, please mannually click above play button to play</div>’;

        console.log("audioPlayPreventedNoticeHtml=%o", audioPlayPreventedNoticeHtml);

        $(".audio_player").append(audioPlayPreventedNoticeHtml);

    }

就可以了:

转载请注明:在路上 » 【已解决】jquery中如何插入html元素

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.178 seconds, using 22.05MB memory