append在jquery中不起作用

时间:2014-02-27 05:46:23

标签: javascript jquery html append

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<body >


   <button onclick="me('${message}');">hello</button>
<div id="play">

</div>
<div id="p">

</div>

</body>
<script type="text/javascript">

   function me(mes)
   {

       switch(mes){
       case 'Knowing our Numbers':



        $("#p").append("<object id='objViewer' name='f'  classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='550' height='400' id='intro to elianas website' align='middle'>"+
                "<param name='allowScriptAccess' value='sameDomain' />"+
                "<param name='movie' width=400px height=400px value='test1.swf' />"+
                "<param name='quality' value='high' />"+
                "<param name='bgcolor' value='#1C140D' />"+
                "<EMBED  src='test1.swf' WIDTH='400px' HEIGHT='400px' quality='high' TYPE='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' NAME='intro' id='player' autostart='false'  allowScriptAccess='sameDomain' ShowControls='1' ShowStatusBar='0' ShowDisplay='0' ></EMBED><br/></object>"
                );
          alert(mes);     

       break;
       case 'Whole Numbers':
           alert(mes);
        break;      

           }

}


</script>

</html>

在上面的代码中我试图在case语句中附加html代码,但它不起作用。我可以使用case语句中的html代码并基于我需要播放视频

提前致谢

5 个答案:

答案 0 :(得分:2)

append()jquery library的方法。要使用此方法,您需要在html页面中加载jQuery library如果您想在没有jQuery库的情况下使用,请尝试以下代码。

var div = document.getElementById('p');
div.innerHTML = div.innerHTML + "<object id='objViewer' name='f'  classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='550' height='400' id='intro to elianas website' align='middle'>"+
                "<param name='allowScriptAccess' value='sameDomain' />"+
                "<param name='movie' width=400px height=400px value='test1.swf' />"+
                "<param name='quality' value='high' />"+
                "<param name='bgcolor' value='#1C140D' />"+
                "<EMBED  src='test1.swf' WIDTH='400px' HEIGHT='400px' quality='high' TYPE='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' NAME='intro' id='player' autostart='false'  allowScriptAccess='sameDomain' ShowControls='1' ShowStatusBar='0' ShowDisplay='0' ></EMBED><br/></object>";

或者你也可以这样使用,

document.getElementById('p').innerHTML+="<object id='objViewer' name='f'  classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='550' height='400' id='intro to elianas website' align='middle'>"+
                    "<param name='allowScriptAccess' value='sameDomain' />"+
                    "<param name='movie' width=400px height=400px value='test1.swf' />"+
                    "<param name='quality' value='high' />"+
                    "<param name='bgcolor' value='#1C140D' />"+
                    "<EMBED  src='test1.swf' WIDTH='400px' HEIGHT='400px' quality='high' TYPE='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' NAME='intro' id='player' autostart='false'  allowScriptAccess='sameDomain' ShowControls='1' ShowStatusBar='0' ShowDisplay='0' ></EMBED><br/></object>";

答案 1 :(得分:1)

您已将${message}作为字符串传递,但在开关语句中,没有与之相关的案例字符串。这就是原因,

<button onclick="me('${message}');">hello</button>
                    ^^^^^^^^^^^^^

试试这个,

 <button onclick="me('Knowing our Numbers');">hello</button>

此外,您需要在此页面中加载 jquery 库。

答案 2 :(得分:0)

.append()jQuery库中的函数。所以,你需要include jQuery in your code。 另外,请务必在.append()函数中包含$(document).ready()函数。

请务必read this快速了解jQuery。

答案 3 :(得分:0)

它似乎你没有包含任何jquery库,所以要么包含一个jquery库来使用.append()你可以从 http://jquery.com/download/ 下载它,或者你也可以这样做简单的javascript。

document.getElementById('p').innerHTML+="<object id='objViewer' name='f'  classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='550' height='400' id='intro to elianas website' align='middle'>"+
                    "<param name='allowScriptAccess' value='sameDomain' />"+
                    "<param name='movie' width=400px height=400px value='test1.swf' />"+
                    "<param name='quality' value='high' />"+
                    "<param name='bgcolor' value='#1C140D' />"+
                    "<EMBED  src='test1.swf' WIDTH='400px' HEIGHT='400px' quality='high' TYPE='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' NAME='intro' id='player' autostart='false'  allowScriptAccess='sameDomain' ShowControls='1' ShowStatusBar='0' ShowDisplay='0' ></EMBED><br/></object>";

答案 4 :(得分:0)

试试这个

<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.9.1.min.js "></script><!--added-->

</head>

<body >


   <button onclick="me('Knowing our Numbers');">hello</button><!--edited-->
<div id="play">

</div>
<div id="p">

</div>

</body>
<script type="text/javascript">

   function me(mes)
   {

       switch(mes){
       case 'Knowing our Numbers':


// 
        // $("#p").append("<object id='objViewer' name='f'  classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='550' height='400' id='intro to elianas website' align='middle'>"+
                // "<param name='allowScriptAccess' value='sameDomain' />"+
                // "<param name='movie' width=400px height=400px value='test1.swf' />"+
                // "<param name='quality' value='high' />"+
                // "<param name='bgcolor' value='#1C140D' />"+
                // "<EMBED  src='test1.swf' WIDTH='400px' HEIGHT='400px' quality='high' TYPE='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' NAME='intro' id='player' autostart='false'  allowScriptAccess='sameDomain' ShowControls='1' ShowStatusBar='0' ShowDisplay='0' ></EMBED><br/></object>"
                // );
               $("#p").append("hiii");
          alert(mes);     

       break;
       case 'Whole Numbers':
           alert(mes);
        break;      

           }

}


</script>

</html>