将文字附加到div

时间:2017-09-08 12:38:41

标签: javascript jquery

我正在尝试使用以下jQuery代码向div添加一些内容。

var myvariable = 'Some Content';
$(".myclass").append(myvariable);
<div class="col-md-12 myclass"></div>

我没有错误但没有任何内容。

我怎样才能让它发挥作用?

4 个答案:

答案 0 :(得分:2)

其他方式是使用.html()

看:http://api.jquery.com/html/

&#13;
&#13;
var myvariable = 'Some Content';

//overwrite content
$(".myclass").html(myvariable); 

//concatenates content
$(".anotherclass").html(
  $(".anotherclass").html() + myvariable
);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-12 myclass"></div>
<div class="col-md-12 anotherclass">
  Already have some content
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

它运作正常。

var myvariable = 'Some Content';
$(".myclass").append(myvariable);
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sample" class="col-md-12 myclass"></div>

答案 2 :(得分:1)

请参阅纯js的代码段:

var myvariable = document.createTextNode('Some Content');
document.getElementsByClassName('myclass')[0].appendChild(myvariable);
<div class="col-md-12 myclass"></div>

请参阅getElementsByClassName返回数组,了解我提供索引的原因。

var myvariable = 'Some Content';
$('.myclass').append(myvariable);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-12 myclass"></div>

答案 3 :(得分:0)

试试这个。

from bottle import route
from bottle import static_file

#Hosts html file which will be invoked from browser.
@route('/filesPath/<staticFile>')
def serve_static_file(staticFile):
    filePath = '/path/to/your/static/file/'
    return static_file(staticFile, filePath)

#host css files which will be invoked implicitly by your html files.
@route('/files_path/css/<cssFile>')
def serve_css_files(cssFile):
    filePath = '/path/to/your/css/file/'
    return static_file(cssFile, filePath)

# host js files which will be invoked implicitly by your html files.
@route('/files_path/js/<jsFile>')
def serve_js_files(jsFile):
    filePath = '/path/to/your/jss/file/'
    return static_file(jsFile, filePath)