将HTML存储到javascript变量中 [英] Storing HTML into a javascript variable

查看:92
本文介绍了将HTML存储到javascript变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用某些JavaScript时遇到了麻烦.我正在使用jQuery动态添加行 到桌子上.此外,JavaScript是通过一些PHP和Kohana代码生成的.

I'm having some trouble with some javascript. I'm using jQuery to dynamiclly add rows to a table. Additionally the javascript is being generated from some PHP and Kohana code.

这是脚本.

<script type="text/javascript">   
    function delExtensionNumber(element){
        $(element).prev().attr('value', 'X'); 
        $(element).parent().parent().hide();
    }

    function addExtensionNumber(){
        lines = <?php echo form::dropdown($ext_id."[did]", $lines, ''); ?>;
        extensions = <?php echo form::dropdown($ext_id."[extension]", $phones, ''); ?>;

        $('#line-extensions > tbody:last').append('<tr><td>'+lines+'</td><td>'+extensions+'</td><td><input type="hidden" name="'+<?php echo $ext_id;?>+'[state]" value="0" /><a class="hide" onclick="delExtensionNumber(this)"></a></td></tr>');

        <?php $ext_id++; ?>
    }
</script>

这在Firefox中工作正常.但是在IE和Chrome中,它不起作用.谷歌浏览器没有任何反应.在IE中,页面上出现错误.该错误是语法错误.它不喜欢<字线后的字符.

This works fine in Firefox. But in IE and Chrome it doesn't work. Nothing happens in google Chrome. In IE I get an error on the page. The error is a syntax error. It doesn't like the < character after the word line.

好的,所以我认为我最好在该字符串两边加上一些引号.但是...当我这样做时,什么都没有.

Ok, so I thought I better put some quotes around this string. But... when I do that nothing works.

推荐答案

我只是假设form::dropdown会吐出纯HTML.在Firefox中,使用 E4X 进行解释,然后在连接时将其转换为字符串. IE和Chrome不支持E4X.要使其正常工作,您可以执行以下操作:

I'm just going to assume that form::dropdown is spitting out plain HTML. In Firefox, this is being interpreted with E4X, and then turned back into a string when concatenated. IE and Chrome do not support E4X. To make it work, you could do something like this:

lines = <?php echo json_encode(form::dropdown(...)); ?>;

编辑:将引号引起来时,它不起作用的原因是HTML包含引号.上面的代码将正确地排除引号.

The reason it's not working when you're putting quotes around it is because the HTML contains quotes. The code above will escape the quotes correctly.

这篇关于将HTML存储到javascript变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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