简体   繁体   English

如何更改 javascript 中的背景文本

[英]How do i change the background text in javascript

</head>
<body>
<div>
<h1>Twhiter</h1>
<!-- Display Value from Text Input-->
<p id="demo"></p>
<!-- Get value from user -->
<input
placeholder="Write Your Tweet Here"
type="text"
id="tweetText"
value=""
name="tweet"
/>
<!-- Error Message-->
<small id="error"></small>
<!--Handle Click to call the function -->
<button onclick="myTweet()">
Twhit!
</button>
</div>
<script src="/index.js"></script>
</body>
</html>

Help me out pls i'm stuck with this code.请帮帮我,我被这段代码困住了。 just help me to figure the problem.... i can't really tell what is the problem.只是帮我解决问题....我真的不知道是什么问题。 Thanks.谢谢。 My.js file is below My.js 文件如下

function myTweet() [
var tweetValue = document.getElementById(tweetText).Value;
var tweetCount = tweetValue.length;
var errorMsg = 
]
// myTweet();

It seems that you are using the wrong brackets for your function.您的 function 似乎使用了错误的括号。 Javascript functions are built this way: Javascript 函数是这样构建的:

function someFunction () {
    // do something
}

Your initialization of the variable errorMsg is also incomplete, you should give it a value.您对变量 errorMsg 的初始化也不完整,您应该给它一个值。

Change your js file based on this, it should look like this:基于此更改您的 js 文件,它应该如下所示:

    function myTweet() {
        var tweetValue = document.getElementById(tweetText).Value;
        var tweetCount = tweetValue.length;
        var errorMsg = "";
    }
// myTweet();

If you want to change the background color for the HTML above you can use如果要更改上面 HTML 的背景颜色,可以使用

<body style="background-color:grey;">

It seems as if you are newbie to JavaScript as neither your syntax is correct nor is your approach, but still here your demanded code -似乎您是 JavaScript 的新手,因为您的语法和方法都不正确,但仍然在这里您需要的代码 -

 function myTweet() { document.getElementById("tweetText").value = "error"; }
 </head> <body> <div> <h1>Twhiter</h1> <.-- Display Value from Text Input--> <p id="demo"></p> <!-- Get value from user --> <input placeholder="Write Your Tweet Here" type="text" id="tweetText" value="" name="tweet" /> <!--Handle Click to call the function --> <button onclick="myTweet()"> Twhit! </button> </div> <script src="/index.js"></script> </body> </html>

With this code you can change the value to "error" on click. 使用此代码,您可以在单击时将值更改为“错误”。

Btw, you can check this link for more info顺便说一句,您可以查看此链接以获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM