JavaScript中的多级引号

Multilevel quote in JavaScript?

本文关键字:多级 JavaScript      更新时间:2023-09-26

好吧,这可能是有史以来最愚蠢的问题,但请耐心等待…

如何做到这一点:

$("#basephoto").after(
  '<tr><td valign="bottom">Additional photo:</td>
       <td>&nbsp;&nbsp;&nbsp;</td>
       <td><div id="addphoto'+curupfld+'" class="browsebg">
         <p class="bpath"></p>
         <input onchange="fillBrowse('"#addphoto'+curupfld+''",this); validateExt(field);"  class="browse transfield" type="file" name="altphoto'+curupfld+'" size="40" />
         <span onclick="delImgField(this.id)" id="delbtn'+curupfld+'" class="abuttons delbtn"></span></div>
       </td>
   </tr>');

感兴趣的部分:

onchange="fillBrowse('"#addphoto'+curupfld+''",this); validateExt(field);"

问题从"onchange"开始。我总是可以创建一个调用这两个函数的函数,解决方案是:

$("#basephoto").after(
  '<tr>
     <td valign="bottom">Additional photo:</td>
     <td>&nbsp;&nbsp;&nbsp;</td>
     <td>
       <div id="addphoto'+curupfld+'" class="browsebg">
       <p class="bpath"></p>
       <input onchange=functionCaller("#addphoto'+curupfld+'",this) class="browse transfield" type="file" name="altphoto'+curupfld+'" size="40" />
       <span onclick="delImgField(this.id)" id="delbtn'+curupfld+'" class="abuttons delbtn"></span>
       </div>
      </td>
   </tr>');

这是可行的,但如果可能的话,我想解决这个问题,而不仅仅是使用变通方法。

我真的不知道你到底在做什么。但是,如果你只是不想用一种更jQuery的方式来写它。。这可能会有所帮助。。

$('#basephoto').append('<tr />');
$('#basephoto tr').append(
    $('<td />')
        .attr('valign','bottom')
        .html('Additional photo:')
);
$('#basephoto tr').append(
    $('<td />')
        .html('&nbsp;&nbsp;&nbsp;')
);
$('#basephoto tr').append(
    $('<td />')
        .html(
            $('<div />')
            .attr({
                "id":"addphoto-"+curupld,
                "class":"browsebg"  
            })
            .html(
                $('<p />')
                    .attr('class','bpath')
                    .html('')
            )
        )
);
$('#addphoto-'+curupfld).append(
    $('<input />')
        .attr({
            "class":"browse transfield",
            "type":"file",
            "name":"altphoto-"+curupfld,
            "size":"40"
        })
        .change(function(){
            functionCaller('#addphoto-'+curupfld,this);
            validateExt(field);
        })
);
$('#addphoto-'+curupfld).append(
    $('<span />')
        .attr({
            "class":"abuttons delbtn",
            "id":"delbtn-"+curupfld
        })
        .click(function(){
            delImgField($(this).attr('id'));
        })
);

查看这篇文章,了解更多如何使用jQuery:创建元素

  • http://mongsang-ga.tumblr.com/post/19914049827/how-to-create-a-element-in-jquery

我想你只需要添加这样的行,然后修复你的问题:

$('#input_that_has_the_change_event').change(function(){
    fillBrowse('#addphoto'+curupfld,this);
    validateExt(field);
});

*附言:别说这很愚蠢。我和你以前一样..:]

使用类似下划线的模板系统。可维护,更易于阅读/故障排除。

http://documentcloud.github.com/underscore/

示例:

    <script id="templateA" type="text/template">
          <p>
                 Test stuff <a href="/home.html"><%= label %></a>
                 ...
          </p>
    </script>
...
<script type="javascript">
        var templateFunction = _.template($('#templateA').html());
        $('#someDivID').after(templateFunction({label: 'Here'}));
</script>