gpt4 book ai didi

Javascript 书签、href 中断?

转载 作者:行者123 更新时间:2023-12-02 18:44:17 24 4
gpt4 key购买 nike

我已经为此烦恼了很长时间,我真的希望有人知道这个问题以及解决它的方法..

我正在生成一个小书签,它工作得非常完美,神奇的是,经过这么多天的 MYSQL 和 PHP 的磨练,我可以这么说。

作为示例,请考虑正在生成此小书签。

javascript:(function(){ a=document.createElement('script'); a.setAttribute('src','//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'); document.body.appendChild(a); data='[["#txtapplicantlname","agrawal","text"],["#txtapplicantfname","aayush","text"],["#txtfather","Ranjan","text"],["#txtmother","Neelam","text"],["#txtPincode","452010","text"],["#txtPhone","2147483647","text"],["#txtEmail","aayush@mail.com","text"],["#rdosex_0,#rdosex_1","1","radio"]]'; for(a=$.parseJSON(data),b=a.length-1;0<=b;b--){ c=a[b]; if (c[2] =='text') { $(c[0]).val(c[1]); } else if (c[2] == 'radio'){ rdata = c[0].split(','); rnum = c[1] - 1; select = rdata[rnum]; $(select).attr('checked', true); } } })();

这是一个闭包编译的书签的重新创建,我无法找到原始版本,因为它从未被保存(长话短说..)所以我想我也可以编辑闭包编译的书签。JSON 文件是这样插入的,因为小书签必须动态生成,并且注定要在 HTTPS 网站上运行,因此无法发送 AJAX 调用。

无论如何,代码工作得很好,但是当我尝试将其作为超链接插入时,它就崩溃了。如果需要的话,代码几乎是这样的

"<a href=" + (Code that was shown above) + "> Drag me to your bookmarks bar!</a>"

它打破了..如果需要的话,这里是打印出来的源代码。

<span class='span6'>Please drag the following button to your bookmarks bar! <br>javascript:(function(){ a=document.createElement('script'); a.setAttribute('src','//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'); document.body.appendChild(a); data='[["#txtapplicantlname","agrawal","text"],["#txtapplicantfname","aayush","text"],["#txtfather","Ranjan","text"],["#txtmother","Neelam","text"],["#txtPincode","452010","text"],["#txtPhone","2147483647","text"],["#txtEmail","aayush@mail.com","text"],["#rdosex_0,#rdosex_1","1","radio"]]'; for(a=$.parseJSON(data),b=a.length-1;0<=b;b--){ c=a[b]; if (c[2] =='text') { $(c[0]).val(c[1]); } else if (c[2] == 'radio'){ rdata = c[0].split(','); rnum = c[1] - 1; select = rdata[rnum]; $(select).attr('checked', true); } } })();<a href='javascript:(function(){ a=document.createElement('script'); a.setAttribute('src','//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'); document.body.appendChild(a); data='[["#txtapplicantlname","agrawal","text"],["#txtapplicantfname","aayush","text"],["#txtfather","Ranjan","text"],["#txtmother","Neelam","text"],["#txtPincode","452010","text"],["#txtPhone","2147483647","text"],["#txtEmail","aayush@mail.com","text"],["#rdosex_0,#rdosex_1","1","radio"]]'; for(a=$.parseJSON(data),b=a.length-1;0<=b;b--){ c=a[b]; if (c[2] =='text') { $(c[0]).val(c[1]); } else if (c[2] == 'radio'){ rdata = c[0].split(','); rnum = c[1] - 1; select = rdata[rnum]; $(select).attr('checked', true); } } })();'>Drag me!</a></span>

它在很多层面上都崩溃了,我已经尝试了很多次来解决它,但每次我进行微小的更改时,它都会再次崩溃。我真的希望有一个更可靠的解决方案,如果有人可以帮助我,我将非常感激..

编辑:这是用 htmlspecialchars 转义双引号后发生的情况

<span class='span6'>Please drag the following button to your bookmarks bar! <br>javascript:(function(){ a=document.createElement('script'); a.setAttribute('src','//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'); document.body.appendChild(a); data='[["#txtapplicantlname","agrawal","text"],["#txtapplicantfname","aayush","text"],["#txtfather","Ranjan","text"],["#txtmother","Neelam","text"],["#txtPincode","452010","text"],["#txtPhone","2147483647","text"],["#txtEmail","aayush@mail.com","text"],["#rdosex_0,#rdosex_1","1","radio"]]'; for(a=$.parseJSON(data),b=a.length-1;0<=b;b--){ c=a[b]; if (c[2] =='text') { $(c[0]).val(c[1]); } else if (c[2] == 'radio'){ rdata = c[0].split(','); rnum = c[1] - 1; select = rdata[rnum]; $(select).attr('checked', true); } } })();<a href='javascript:(function(){ a=document.createElement('script'); a.setAttribute('src','//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'); document.body.appendChild(a); data='[[&quot;#txtapplicantlname&quot;,&quot;agrawal&quot;,&quot;text&quot;],[&quot;#txtapplicantfname&quot;,&quot;aayush&quot;,&quot;text&quot;],[&quot;#txtfather&quot;,&quot;Ranjan&quot;,&quot;text&quot;],[&quot;#txtmother&quot;,&quot;Neelam&quot;,&quot;text&quot;],[&quot;#txtPincode&quot;,&quot;452010&quot;,&quot;text&quot;],[&quot;#txtPhone&quot;,&quot;2147483647&quot;,&quot;text&quot;],[&quot;#txtEmail&quot;,&quot;aayush@mail.com&quot;,&quot;text&quot;],[&quot;#rdosex_0,#rdosex_1&quot;,&quot;1&quot;,&quot;radio&quot;]]'; for(a=$.parseJSON(data),b=a.length-1;0&lt;=b;b--){ c=a[b]; if (c[2] =='text') { $(c[0]).val(c[1]); } else if (c[2] == 'radio'){ rdata = c[0].split(','); rnum = c[1] - 1; select = rdata[rnum]; $(select).attr('checked', true); } } })();'>Drag me!</a></span>

最佳答案

请使用

document.getElementsByTagName('head').item(0).appendChild(a);

而不是你使用的

document.body.appendChild(a); 

我希望它能运作良好。

关于Javascript 书签、href 中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16560613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com