我如何用javaScript生成正确的html标签? [英] how I can generate Properly html tag with javaScript ?

查看:48
本文介绍了我如何用javaScript生成正确的html标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建此代码以替换内容并显示按钮:

  var  text = $(item).html()。replace(exp, < button onclick = 'alert('hi')'id ='btnac'href ='$ 1'>点击接受语音聊天< / button>); 





但是在页面中我得到了这个并且我无法对任何功能发出警告:



 <  按钮    onclick   =  alert(    hi')'  =      id   = <跨越ss =code-keyword> btnac    href   =  www.example.com > 点击接受语音聊天<   / button  >  

解决方案

(item).html()。replace(exp, < span class =code-string>< button onclick ='alert('hi')'id ='btnac'href ='


1'>点击接受语音聊天< ; / button>);





但在页面中我得到了这个,我无法显示任何功能的警告:< br $> b $ b

 <  按钮    onclick   = <小号pan class =code-keyword> alert(    hi')'  =     id   =  btnac    href   =  www.example.com  > 点击接受语音聊天<   /按钮 >  


而不是 onclick =alert(hi')'=,它应该是 onclick =alert('hi')。无论如何,1)在生产中使用 alert 是一个坏主意,2) alert 不为用户提供方法确认。



更好用,例如,jQuery对话框: http://jqueryui.com /对话框 [ ^ ]。



DOM操作也应该更准确地完成。即使你的工作正常,它也会使功能难以支持,因为它过分依赖于现有内容和 exp 。通过其订单找到的节点,删除它并替换您需要的节点(我不知道为什么)会更可靠。请参阅 http://api.jquery.com/category/manipulation [ ^ ]。



-SA

Hi
I Create this code for replace something and show a button :

var text = $(item).html().replace(exp, "<button onclick='alert('hi')' id='btnac' href='$1' >Click to Accept Voice Chat</button>");



But in page I get this and I cant show alert on any function :

<button onclick="alert(" hi')'="" id="btnac" href="www.example.com">Click to Accept Voice Chat</button>

解决方案

(item).html().replace(exp, "<button onclick='alert('hi')' id='btnac' href='


1' >Click to Accept Voice Chat</button>");



But in page I get this and I cant show alert on any function :

<button onclick="alert(" hi')'="" id="btnac" href="www.example.com">Click to Accept Voice Chat</button>


Instead of onclick="alert(" hi')'="", it should be onclick="alert('hi')". Anyway, 1) using alert in production is a bad idea, 2) alert doesn't provide a way for user confirmation.

Better use, for example, jQuery dialog: http://jqueryui.com/dialog[^].

DOM manipulation also should be done more accurately. Even if yours works, it makes the functionality poorly supportable, because it depends too much on existing content and your exp. It would be more reliable to take a node found by its order, remove it, and replace what you need (I don't know why though). Please refer to http://api.jquery.com/category/manipulation[^].

—SA


这篇关于我如何用javaScript生成正确的html标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆