Change value of div using jquery .val() not working

坚强是说给别人听的谎言 提交于 2021-02-11 12:40:34

问题


I have this div:

<div id="libraryDisplay">asdf</div>

and this code:

$('#libraryDisplay').val(booksList);

and booksList is a string. It's still displaying "asdf". Any suggestions?


回答1:


val is for form elements. If you're trying to set the text of the <div>, use text instead:

$('#libraryDisplay').text(booksList);

If booksList is actually an HTML fragment (and you're reasonably sure it's coming from a safe source and doesn't have the potential to corrupt your page) use html:

$('#libraryDisplay').html(booksList);



回答2:


Try

$('#libraryDisplay').html(booksList);



来源:https://stackoverflow.com/questions/21294992/change-value-of-div-using-jquery-val-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!