Javascript 如何在JQuery中使用appendTo()函数显示此小部件?
我有这段代码&我已经厌倦了使用JQuery的appendTo()函数来显示这个小部件……我缺少了一些东西,但是我在锁定它时遇到了麻烦 以下是原始代码,请参见以下内容:Javascript 如何在JQuery中使用appendTo()函数显示此小部件?,javascript,jquery,Javascript,Jquery,我有这段代码&我已经厌倦了使用JQuery的appendTo()函数来显示这个小部件……我缺少了一些东西,但是我在锁定它时遇到了麻烦 以下是原始代码,请参见以下内容: var theText = new Array(); theText[0] = "David Footitt is absolutely delighted with them and the service he received.<br /><br />Regtransfers are definite
var theText = new Array();
theText[0] = "David Footitt is absolutely delighted with them and the service he received.<br /><br />Regtransfers are definitely the first port of call whenever I or my colleagues are looking for a special number plate, he said.";
theText[1] = "What was Prakash Patel's experience with Regtransfers?<br /><br />Great service, always keeping us up to date with related plates, transfers done very easily and value for money.";
theText[2] = "4 MBE is one the best investments that I have made in recent years.<br /><br />Thanks to Regtransfers for making it such a simple and straightforward process. It's definitely got me thinking about others for the business.";
var links = new Array();
links[0] = 'http://www.regtransfers.co.uk/number-plates-stories/new51.asp';
links[1] = 'http://www.regtransfers.co.uk/number-plates-stories/oo08cty.asp';
links[2] = 'http://www.regtransfers.co.uk/main/stories/4mbe.asp';
var title = new Array();
title[0] = '<strong>David Footitt</strong><br />(News Transport Ltd)<br />NEW 51';
title[1] = '<strong>Prakash Patel</strong><br />(City Inter-Rent)<br />OO08 CTY';
title[2] = '<strong>Sandeep Patel</strong><br />(Ambe Medical Group)<br />4 MBE';
var images = new Array();
images[0] = '/images_new/rotatingTestimonials/new51.jpg';
images[1] = '/images_new/rotatingTestimonials/oo08cty.jpg';
images[2] = '/images_new/rotatingTestimonials/4mbe.jpg';
var j = 0
var p = theText.length;
var whichImage = Math.round(Math.random()*(p-1));
document.write('<div style="padding:3px; border:1px solid #CCC;"><div style="background-image:url(/images_new/backgrounds/grey_gradient.jpg); background-repeat:repeat-x; text-align:center; font-weight:bold; font-size:90%; padding:5px;">Satisfied Customer</div><p align="center" style="font-size:11px;">' + title[whichImage] + '</p><p align="center" style="font-size:11px;"><img src="' + images[whichImage] + '" alt="Customer Testimonials" style="width:140px" /></p><p align="left" style="font-size:11px;">' + theText[whichImage] + '</p><p align="right" style="font-size:11px;"><a href="' + links[whichImage] + '">read more ...</a></p></div>');
var theText=new Array();
文本[0]=“David Footitt对他们和他所获得的服务感到非常高兴。
他说,无论何时我或我的同事需要一个特殊的号码牌,Regtransfers绝对是第一个停靠港。”;
文本[1]=“Prakash Patel在RegTransfer方面的经验是什么?
优质服务,始终让我们了解相关车牌的最新信息,轻松完成转账,物有所值。”;
文本[2]=“4 MBE是我近年来做的最好的投资之一。
感谢Regtransfers使其成为一个如此简单和直接的过程。这无疑让我想到了其他业务。”;
var links=新数组();
链接[0]='http://www.regtransfers.co.uk/number-plates-stories/new51.asp';
链接[1]='http://www.regtransfers.co.uk/number-plates-stories/oo08cty.asp';
链接[2]='http://www.regtransfers.co.uk/main/stories/4mbe.asp';
var title=新数组();
标题[0]=“David Footitt
(新闻运输有限公司)
新51”;
标题[1]=“Prakash Patel
(城市租金)
OO08 CTY”;
标题[2]=“Sandeep Patel
(安贝医疗集团)
4 MBE”;
var images=新数组();
images[0]='/images_new/rotatingestimations/new51.jpg';
images[1]='/images_new/rotatingestimations/oo08cty.jpg';
images[2]='/images_new/rotatingestimationals/4mbe.jpg';
var j=0
var p=文本长度;
var whichImage=Math.round(Math.random()*(p-1));
文档.书写('successful Customer”+title[whichImage]+”
”+文本[whichImage]+”
);
我想如果我把最后一行改成:
$('<div...long html string with the other variables').appendTo($('#rotate-testimonials'));
$(“您应该使用选择器,而不是文本。将大量文本放在DIV的选择器中,而不是DIV内容本身,您应该不会有问题
<div id='theDiveID'>long html string with the other variables</div>
$('#theDivID').appendTo('#rotate-testimonials');
包含其他变量的长html字符串
$(“#theDivID”)。附加到(“#旋转证明”);
你应该使用选择器,而不是文本。为你的DIV输入选择器,用大量文本代替DIV内容本身,你应该会没事的
<div id='theDiveID'>long html string with the other variables</div>
$('#theDivID').appendTo('#rotate-testimonials');
包含其他变量的长html字符串
$(“#theDivID”)。附加到(“#旋转证明”);
从根本上说,它是有效的,但我怀疑您想替换轮换推荐信
内容,而不是附加(添加)到内容中。因此最后一行是:
$('#rotate-testimonials').html(...long string...);
编辑:您已经说过,您这边的问题是,DIV#rotate commissional
中没有显示任何内容。即使使用原始代码,您也应该看到一些内容,因此这里有一些内容需要检查:
您是否在DOM中存在div#rotate commentials
之前运行代码?例如,div
上方的脚本是否未包装在处理程序或类似程序中?这是一个容易犯的错误。必须先存在div,然后才能对其进行写入,并立即运行脚本。在我上面的示例中,请注意,所有内容都是wrapp在jQuery(函数($){…});
结构中调用,直到DOM准备好被操作为止。您可以这样做,也可以将脚本放在页面的最末尾,就在
标记之前
您是否真的有一个带有id=“rotate estimentials”
的div?例如,没有打字错误或类似错误,它是id
而不是名称
,等等
是否只有一个元素或其他全局对象具有该名称?(检查的简单方法:在id=“…”中将名称更改为“fluglehorn”?)
在标记和脚本中。如果它开始工作,这意味着您有另一个名为rotate Certifications
的地方。当然,这假设您没有任何名为fluglehorn
的东西
如果不是这些,我就没有主意了,但希望将您的想法与上面的工作版本进行比较会有所帮助
主题外:也就是说,一点重构可以帮助更容易地向推荐信等中添加条目。与并行数组相比,我会使用对象数组,每个信息位(文本、标题、链接、图像)都有一个属性。此外,还可以使用数组和对象文字符号(而不是新数组()
然后是一堆作业
以下是一个仅更改为数组文字符号的版本:
var theText = [
"David Footitt is absolutely delighted with them and the service he received.<br /><br />Regtransfers are definitely the first port of call whenever I or my colleagues are looking for a special number plate, he said.",
"What was Prakash Patel's experience with Regtransfers?<br /><br />Great service, always keeping us up to date with related plates, transfers done very easily and value for money.",
"4 MBE is one the best investments that I have made in recent years.<br /><br />Thanks to Regtransfers for making it such a simple and straightforward process. It's definitely got me thinking about others for the business."
];
var links = [
'http://www.regtransfers.co.uk/number-plates-stories/new51.asp',
'http://www.regtransfers.co.uk/number-plates-stories/oo08cty.asp',
'http://www.regtransfers.co.uk/main/stories/4mbe.asp'
];
var title = [
'<strong>David Footitt</strong><br />(News Transport Ltd)<br />NEW 51',
'<strong>Prakash Patel</strong><br />(City Inter-Rent)<br />OO08 CTY',
'<strong>Sandeep Patel</strong><br />(Ambe Medical Group)<br />4 MBE'
];
var images = [
'/images_new/rotatingTestimonials/new51.jpg',
'/images_new/rotatingTestimonials/oo08cty.jpg',
'/images_new/rotatingTestimonials/4mbe.jpg'
];
var j = 0
var p = theText.length;
var whichImage = Math.round(Math.random()*(p-1));
$('#rotate-testimonials').html(
'<div style="padding:3px; border:1px solid #CCC;"><div style="background-image:url(/images_new/backgrounds/grey_gradient.jpg); background-repeat:repeat-x; text-align:center; font-weight:bold; font-size:90%; padding:5px;">Satisfied Customer</div><p align="center" style="font-size:11px;">' + title[whichImage] + '</p><p align="center" style="font-size:11px;"><img src="' + images[whichImage] + '" alt="Customer Testimonials" style="width:140px" /></p><p align="left" style="font-size:11px;">' + theText[whichImage] + '</p><p align="right" style="font-size:11px;"><a href="' + links[whichImage] + '">read more ...</a></p></div>');
var theText=[
“大卫·福伊特(David Footitt)对他们和他所获得的服务感到非常高兴。
无论何时我或我的同事想要一个特殊的车牌,Regtransfers绝对是第一个停靠港,他说。”,
“Prakash Patel在RegTransfer方面的经验是什么?
很棒的服务,始终让我们了解相关车牌的最新信息,转账非常容易,而且物有所值。”,
“4 MBE是我近年来进行的最好的投资之一。
感谢Regtransfers使其成为一个如此简单和直接的过程。这无疑让我想到了其他业务。”
];
变量链接=[
'http://www.regtransfers.co.uk/number-plates-stories/new51.asp',
'http://www.regtransfers.co.uk/number-plates-stories/oo08cty.asp',
'http://www.regtransfers.co.uk/main/stories/4mbe.asp'
];
变量标题=[
“David Footitt
(新闻运输有限公司)
新51”,
“Prakash Patel
(城市租金)
OO08 CTY”,
“Sandeep Patel
(安贝医疗集团)
4 MBE”
];
var图像=
$('#rotate-testimonials').append('...')