这篇文章主要围绕如何在textarea(HTMLTag)中编写JavaScript对象以使用documentQuerySelector获取该对象展开,旨在为您提供一份详细的参考资料。我们将全面介绍如何
这篇文章主要围绕如何在 textarea(HTML Tag) 中编写 JavaScript 对象以使用 documentQuerySelector 获取该对象展开,旨在为您提供一份详细的参考资料。我们将全面介绍如何在 textarea(HTML Tag) 中编写 JavaScript 对象以使用 documentQuerySelector 获取该对象,同时也会为您带来
- 如何在 textarea(HTML Tag) 中编写 JavaScript 对象以使用 documentQuerySelector 获取该对象
部分属性与 vue 版本的兼容问题 - 和标签
与 上的 JS innerHTML - Angular 6 readonly textarea 不会从第二个 textarea 绑定
如何在 textarea(HTML Tag) 中编写 JavaScript 对象以使用 documentQuerySelector 获取该对象
如何解决如何在 textarea(HTML Tag) 中编写 JavaScript 对象以使用 documentQuerySelector 获取该对象
我想在 Textarea 或输入字段中编写 javascript 对象,并想使用 documentQuerySelector 检索这些数据。但是当我这样做时它返回一个字符串。
有没有办法在浏览器中编写 javascript 代码并从我的 javascript 文件(如 app.js)访问该代码
HTML
<textarea />
我使用事件作为反应中的值
JS / 反应 我
e.target.value
我在文本区写的对象
{
quizTitle: "React Quiz Component Demo",quizSynopsis: "Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes,nascetur ridiculus mus. Donec quam felis,ultricies nec,pellentesque eu,pretium quis,sem. Nulla consequat massa quis enim",questions: [
{
question: "How can you access the state of a component from inside of a member function?",questionType: "text",questionPic: "https://dummyimage.com/600x400/000/fff&text=X",// if you need to display Picture in Question
answerSelectionType: "single",answers: [
"this.getState()","this.prototype.stateValue","this.state","this.values"
],correctAnswe": "3",messageForCorrectAnswer: "Correct answer. Good job.",messageForIncorrectAnswer: "Incorrect answer. Please try again.",explanation: "Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",point: "20"
}
]
}
但是当我检索数据和 console.log 时,它给出字符串而不是 js 对象
"{
quizTitle: "React Quiz Component Demo",point: "20"
}
]
}"
解决方法
在这种情况下 textarea 的 event.target.value
将返回一个字符串
如果你想要这个对象,你必须使用JSON.parse(textValue)
来获取它
此外,传递给 textValue
的 JSON.parse
必须是有效的 JSON(您可以查看 MDN doc 以了解有关有效 JSON 的详细信息)
例如,要获取它,您可以声明一个对象并使用 JSON.stringify(obj)
获取有效的 JSON。
const a = {
quizTitle: "React Quiz Component Demo",quizSynopsis:
"Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes,nascetur ridiculus mus. Donec quam felis,ultricies nec,pellentesque eu,pretium quis,sem. Nulla consequat massa quis enim",questions: [
{
question:
"How can you access the state of a component from inside of a member function?",questionType: "text",questionPic: "https://dummyimage.com/600x400/000/fff&text=X",// if you need to display Picture in Question
answerSelectionType: "single",answers: [
"this.getState()","this.prototype.stateValue","this.state","this.values",],correctAnswer: "3",messageForCorrectAnswer: "Correct answer. Good job.",messageForIncorrectAnswer: "Incorrect answer. Please try again.",explanation:
"Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",point: "20",},};
console.log(JSON.stringify(a,null,2));
{
"quizTitle": "React Quiz Component Demo","quizSynopsis": "Lorem ipsum dolor sit amet,"questions": [
{
"question": "How can you access the state of a component from inside of a member function?","questionType": "text","questionPic": "https://dummyimage.com/600x400/000/fff&text=X","answerSelectionType": "single","answers": [
"this.getState()","this.values"
],"correctAnswer": "3","messageForCorrectAnswer": "Correct answer. Good job.","messageForIncorrectAnswer": "Incorrect answer. Please try again.","explanation": "Lorem ipsum dolor sit amet,"point": "20"
}
]
}
获得有效的 JSON 字符串后,将其粘贴到 textarea 中,当您从 textarea 获取值时,只需 JSON.parse()
该值即可获取您的对象值
export default function App() {
const [value,setValue] = React.useState('''')
const handleClick = () => {
try {
console.log(JSON.parse(value))
alert(''check the console log'')
} catch(err) {
console.error(err)
alert(''not a valid JSON'')
}
}
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<textarea rows="30" cols="60" value={value} onChange={e => setValue(e.target.value)}/>
<br/>
<button onClick={handleClick}>Get JSON</button>
</div>
);
}
部分属性与 vue 版本的兼容问题
【01】标签使用按钮样式
<el-tag effect="dark" v-if="myhotelinfo.runstatus==''T''" type="success">正常</el-tag>
<el-tag effect="dark" v-else type="danger">异常</el-tag>
//需要版本至少"element-ui": "^2.9.2"(packjson.json文件中)
//如果已经安装了低版本,则需要先卸载npm uninstall element-ui,再重新安装指定版本npm instal element-ui@2.9.2 -S
【02】组标签卡死问题
Q:使用el-tags浏览器卡死的情况
A:element版本过高,而vue版本过低,需要降低elemet版本
参考:https://www.cnblogs.com/wangmaoling/p/11102573.html
和标签
和标签有区别吗?">和标签有区别吗?
1.为什么做输入框用
<input type="textarea">
焦点是居中的,怎么实现用input,使焦点也在左上角?
用
1.焦点就默认左上角
2.用
回复讨论(解决方案)
直接用textarea,,,表单能正确获取内容。。。没有必要用label吧。。。
以上就是<input type="textarea">和标签
与 上的 JS innerHTML
如何解决<tag/> 与 <tag></tag> 上的 JS innerHTML
我有以下 HTML:
<html>
<body>
<b /> <br>
Lets say it again: <b /> <br>
</body>
<script>
// this runs after document has already been loaded
if (document.getElementsByTagName(''b'').length > 0) {
Array.from(document.getElementsByTagName(''b'')).forEach((ele) => {
ele.innerHTML = "Hello World!";
});
}
<script>
</html>
结果是只有第一个 <b />
修改了其 innerHTML。并且没有任何内容显示出来。检查显示第一个 <b />
但是,如果我将 <b />
替换为 <b></b>
,则一切正常。
知道为什么或如何修复它吗?我正在使用一个标签来放置一个变量,我希望它很短,因为我有很多。
谢谢!
Angular 6 readonly textarea 不会从第二个 textarea 绑定
如何解决Angular 6 readonly textarea 不会从第二个 textarea 绑定
使用 Angular 6 和 Material 控件,我在只读文本区域中显示我的数据(字符串数组)。我有第二个可编辑文本区域,我想在其中输入文本,然后使用按钮将第二个区域中的文本动态添加到(第一个)只读文本区域。
我也尝试过单向绑定和插值。调试的时候可以看到我的新文本被推送到了数据字符串数组中。无论如何,更新后的字符串数组不会显示在第一个文本区域中。
this.comments: string[];
this.newComment: string;
this.comments = data;
this.newComment = '''';
addNewComment(newComment: string) {
if (newComment.length > 0) {
this.comments.push(newComment + ''\\n'');
this.newComment = '''';
this.saModified = true;
}
}
<mat-form-field>
<textarea matInput id=''newComment'' name=''newComment'' [disabled]="!userHasRole(''Admin'')" rows=''8'' cols=''15'' style=''text-align: left; border:solid 1px black;'' [(ngModel)]=''newComment''></textarea>
</mat-form-field>
<button type=''button'' mat-raised-button color=''primary'' name=''add-new-comment'' (click)=''addNewComment(newComment)'' [disabled]=!userHasRole(''Admin'') || loading">Add Comment</button>
<mat-form-field>
<textarea matInput readonly id=''comments'' name=''comments'' [disabled]="!userHasRole(''Admin'')" rows=''8'' cols=''15'' style=''text-align: left; border:solid 1px black;'' [ngModel]=''comments''></textarea>
</mat-form-field>