在javascript中编写html时引用的问题
Issue with quotations while writing html in javascript
我正在编写javascript中的html结构,我需要在单击链接时返回。我是这样写的
return '<div class="contactForm">'+
'<input type="text" name="contactName" onBlur="if (this.value == "") {this.value == "Name";}" onFocus="if ((this.value == "") || (this.value == "Name")) {this.value = "";};" value="Name" style="">'+
当我在页面上看到它时,onBlur
属性出现在红色颜色中,引号放错了地方。
<input name="contactName" onblur="if (this.value == " ")="" {this.value="Name" ;}"="" onfocus="if ((this.value == " ||="" (this.value="=" "name"))="" ;};"="" value="Name" style="" type="text">
我怎么写返回部分,使我得到正确的HTML页面?我相信这是由于引号放错了地方。我在页面源中看到这个错误在期望属性名
您需要使用''':对字符串内部的引号进行双转义,一次用于字符串本身,一次用于内联JavaScript。
return '<div class="contactForm">'+
'<input type="text" name="contactName" onBlur="if (this.value == '''"'''") {this.value == '''"Name'''";}" onFocus="if ((this.value == '''"'''") || (this.value == '''"Name'''")) {this.value = '''"'''";};" value="Name" style="">'+
如果你想让它更清晰一点,你可以使用单引号,只需要转义一次。
return '<div class="contactForm">'+
'<input type="text" name="contactName" onBlur="if (this.value == '''') {this.value == ''Name'';}" onFocus="if ((this.value == '''') || (this.value == ''Name'')) {this.value = '''';};" value="Name" style="">'+
var element = '<div class="contactForm">';
element += '<input type="text" name="contactName" onBlur="if (this.value == '''') {this.value == ''Name'';}" onFocus="if ((this.value == '''') || (this.value == ''Name'')) {this.value = '''';};" value="Name" style="">';
element += '</div>';
return element;
相关文章:
- JavaScript变量引用数组时出现问题
- 性能问题:存储对DOM元素的引用与使用选择器相比
- Javascript-通过引用传递变量问题
- 我应该如何在JS中使用揭示模块模式,传递引用,并避免加载顺序问题
- 全球不断改变价值.可能通过引用或范围问题传递?帮助!链接中的详细示例
- 从函数中引用对象时遇到问题 - Adobe Animate canvas
- ExtJS 4.1 :建立对对象的引用时出现问题
- 在循环中创建的函数中使用时出现数组引用问题
- JavaScript <-> DOM 循环引用问题的精确解释
- 使用Foundation的Javascript源代码引用问题
- 节点需要路径引用问题
- jQuery引用问题与函数在OnClick
- JavaScript变量引用问题
- JS作用域/引用问题
- 解决了angular中指令包装中的圆形引用问题
- 反应引导元素的“引用”问题
- 陷入了一个简单的对象文字范围/引用问题
- JavaScript 循环引用问题
- Asmx Web服务引用问题
- 使用AngularJS "copy()"以避免引用问题