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

【已解决】jquery的js中如何设置html元素的值

JS crifan 825浏览 0评论
折腾:
【已解决】本地搭建前端Web页面实现机器人问答的产品演示
期间,需要去搞懂,js或jquery中,如何设置html元素的值。
set value of html element js
JavaScript DOM HTML
好像是用:
innerHTML
HTML DOM Input Text value Property
Javascript Get Element by Id and set the value – Stack Overflow
JavaScript/Changing elements – Wikibooks, open books for an open world
jquery set element value
jQuery Set Content and Attributes
$("#btn1").click(function(){
    $("#test1").text("Hello world!");
});
$("#btn2").click(function(){
    $("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
    $("#test3").val("Dolly Duck");
});
.val() | jQuery API Documentation
javascript – How to set value of input text using jQuery – Stack Overflow
How to get and set form element values with jQuery – Electric Toolbox
【总结】
此处如果只是使用js,则是:
getElement后,再去设置对应的属性,比如innerHTML,src等等:
  • text() – Sets or returns the text content of selected elements
  • html() – Sets or returns the content of selected elements (including HTML markup)
  • val() – Sets or returns the value of form fields
<h1 id="id01">Old Heading</h1>

<script>
var element = document.getElementById("id01");
element.innerHTML = "New Heading";
</script>

<img id="myImage" src="smiley.gif">
<script>
document.getElementById("myImage").src = "landscape.jpg";
</script>

<input type="text" id="myText" value="Mickey”>
document.getElementById("myText").value = "Johnny Bravo”;

var mylist = document.getElementById("myList");
document.getElementById("favorite").value = mylist.options[mylist.selectedIndex].text;


myButton.type = "text";
而对于jquery来说,则是:
$(“your_selector”)
然后设置对应的text,html,val
<script src="
https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
"></script>
<script>
$(document).ready(function(){
    $("#btn1").click(function(){
        $("#test1").text("Hello world!");
    });
    $("#btn2").click(function(){
        $("#test2").html("<b>Hello world!</b>");
    });
    $("#btn3").click(function(){
        $("#test3").val("Dolly Duck");
    });
});
</script>

<p id="test1">This is a paragraph.</p>
<p id="test2">This is another paragraph.</p>

<p>Input field: <input type="text" id="test3" value="Mickey Mouse"></p>

<button id="btn1">Set Text</button>
<button id="btn2">Set HTML</button>
<button id="btn3">Set Value</button>
还可以设置属性:
$("button").click(function(){
    $("#w3s").attr("href", "https://www.w3schools.com/jquery/");
});
详见:
jQuery Set Content and Attributes

转载请注明:在路上 » 【已解决】jquery的js中如何设置html元素的值

发表我的评论
取消评论

表情

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

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