我在PHP页面中使用'createDivs'类列出了17个块。当我按下键盘的输入按钮以在HTML源中搜索元素以查看它是否具有名为“blocksList div_1_1”,“blocksList”的类时,我试图在每个块中获取每个块的文本数据div_2_1'或者是整数,所以我可以用每个块上的文字显示警告信息。
var current_col = 1;
var current_row = 1;
var totalrowcount = 17; //is using for how many blocks of rows I have in total
$(document).ready(function()
{
$("div").show();
$(".div_"+current_col+'_'+current_row).addClass( "yellowbg" );
getBlocks();
});
var current_top_idx = 0;
$(document).keyup(function(event)
{
var yellowbg = $(".yellowbg");
var rowwidth = $(".row").css( "width").split("px");
var displayrowcount = 7; //is using for to hide other blocks
var rowheight = 49; //is using for to make measurement from row 1 to row 7 to hide the other blocks
var totalwidth_current = 0;
var totalwidth_current1 = 0;
if(parseInt(current_row)+parseInt(1) <= 8)
{
for(var i=1; i<=(parseInt(current_row)+parseInt(1));i++ )
{
var yellowbgnextdivwidth = $(".div_"+current_col+'_'+i).css( "width");
var yellowbgnextdivwidthsplit = yellowbgnextdivwidth.split("px");
totalwidth_current=parseInt(totalwidth_current)+parseInt(yellowbgnextdivwidthsplit[0]);
}
}
if (event.keyCode == 13)
{ //enter
alert("enter tested");
var $row = $('.blocksList div_'+i+'1');
alert($row);
}
else if (event.keyCode == 38)
{ //up
if(current_col > 1 && current_row == 1)
{
var currentrowtop = $(".mainWrap div.row:first-child").css( "margin-top");
var calcuatedrowtop = -(current_col*rowheight);
currentrowtop = parseInt(currentrowtop)-rowheight;
if((currentrowtop - calcuatedrowtop) < rowheight)
{
currentrowtop = parseInt(currentrowtop)+rowheight;
currentrowtop = parseInt(currentrowtop)+rowheight;
var rowtop = currentrowtop+="px";
$(".mainWrap div.row:first-child").css( "margin-top", rowtop );
if (current_top_idx > 0) { current_top_idx--; }
}
current_col--;
}
}
else if (event.keyCode == 40)
{ //down
if(current_col < totalrowcount && current_row == 1)
{
if (current_col >= current_top_idx + displayrowcount)
{
var currentrowtop = $(".mainWrap div.row:first-child").css( "margin-top");
currentrowtop = parseInt(currentrowtop)-rowheight;
var rowtop = currentrowtop+="px";
$(".mainWrap div.row:first-child").css( "margin-top", rowtop );
current_top_idx++;
}
current_col++;
}
}
$('div').removeClass( "yellowbg" );
//alert(".div_"+current_col+'_'+current_row);
$(".div_"+current_col+'_'+current_row).addClass( "yellowbg" );
});
width = new Array();
imagecontent = new Array();
pgmcontent = new Array();
var checksuminit = 0;
var checksum = 0;
function getBlocks()
{
var i=0;
$.ajax({
url:'database.php',
type:'POST',
data:'',
success: function(result)
{
var $doc = $(result);
i=1;
$doc.filter('p#blocks').each(function(index) {imagecontent[i] = $(this).html();i++;});
createDivs(i-1);
totalrowcount = i-1;
var j=1;
$doc.filter('#links').each(function(index) {
var $link = $(this).html();
$link1 = $link.replace("&", "&");
if($.trim($link1) !='')
{
checksuminit+=j;
getlinks($link1,j);
}
j++;
});
}
});
}
function createDivs(numberOfDivs)
{
var programsNumber = 1;
for(var i=1;i<=numberOfDivs;i++)
{
$('.mainWrap').append(
'<div class="row" id="row'+i+'">' +
'<div id="image'+i+'" class="blocksList div_'+i+'_1"></div>' +
'<div class="rowSubPgm">' +
'<div id="block'+(programsNumber++)+'" class="pgmFirstRow div_'+i+'_2 row2"></div>' +
'<div id="block'+(programsNumber++)+'" class="pgmFirstRow div_'+i+'_3 row3"></div>' +
'</div>' +
'</div><div class="clear"></div>');
}
$(".div_"+current_col+'_'+current_row).addClass( "yellowbg" );
}
function getlinks($link,j)
{
var widthval = 850;
var parts = $link.split("/");
var links = parts[parts.length-1];
var programlength = 0;
$.ajax({
url:$.trim(links),
type:'GET',
data:'',
success: function(data)
{
var $data = $(data);
var title1 = $data.filter("#title1").html();
var currenttotal = 0;
var firstele = ((j-1)*0)+0; // how many blocks i want to output in per block
var lastele = parseInt(firstele)+0; // how many blocks i want to output AFTER the first block
var k = 1;
var programlength = 0;
for(;firstele <= lastele;firstele++)
{
currenttotal++;
pgmcontent[firstele] = eval('title'+k);
k++;
}
checksum+=j;
if(checksuminit == checksum)
{
for(var kk=1;kk <= imagecontent.length-1;kk++)
{
$('#image'+kk).html(imagecontent[kk]);
}
$("div").show();
}
}
});
}
在这种情况下,当我按下键盘的向下箭头按钮,直到我可以进入第6块并按下回车按钮时,它应该在警告信息上显示“Block 6”,但我得到了显示空对象的警告消息,它表示“[对象对象]”。当我按下回车键时,我试图找出如何提取每个块上的文本数据以显示在警报消息上。
以下是HTML的示例:
<div class="mainWrap">
<div class="row" id="row1"><div id="image1" class="blocksList div_1_1 yellowbg">Block 1</div>
<div class="rowSubPgm"><div id="block1" class="pgmFirstRow div_1_2 row2"></div>
<div id="block2" class="pgmFirstRow div_1_3 row3"></div></div></div><div class="clear></div>
<div class="row" id="row2"><div id="image2" class="blocksList div_2_1">Block 2</div>
<div class="rowSubPgm"><div id="block3" class="pgmFirstRow div_2_2 row2"></div>
<div id="block4" class="pgmFirstRow div_2_3 row3"></div></div></div><div class="clear"></div>
<div class="row" id="row3"><div id="image3" class="blocksList div_3_1">Block 3</div>
<div class="rowSubPgm"><div id="block5" class="pgmFirstRow div_3_2 row2"></div>
<div id="block6" class="pgmFirstRow div_3_3 row3"></div></div></div><div class="clear"></div>
<div class="row" id="row4"><div id="image4" class="blocksList div_4_1">Block 4</div>
<div class="rowSubPgm"><div id="block7" class="pgmFirstRow div_4_2 row2"></div>
如果你需要看看,这是小提琴:http://jsfiddle.net/VZWNE/21/
当我通过显示第6块来获取警报消息时,您能否告诉我如何在另一个php页面中搜索该元素,直到找到匹配元素来提取href链接?
EG当我按下按钮直到我在块6上时,它将搜索带有元素的类以提取块6文本并在另一个PHP源上搜索元素以找到块6的匹配然后找到提取它的href链接,它将显示“http://www.hereisthelink.com/test.asx”。我怎么能这样做?
这是HTML源代码:
<p id='blocks'>Block 1<p id='links'>http://www.testsite.com.com/myscript.php?blocks=Block 1</p><a href="">Link 1</a>
<p id='blocks'>Block 2<p id='links'>http://www.testsite.com.com/myscript.php?blocks=Block 2</p><a href="">Link 1</a>
<p id='blocks'>Block 3<p id='links'>http://www.testsite.com.com/myscript.php?blocks=Block 3</p><a href="">Link 1</a>
<p id='blocks'>Block 4<p id='links'>http://www.testsite.com.com/myscript.php?blocks=Block 4</p><a href="">Link 1</a>
<p id='blocks'>Block 5<p id='links'>http://www.testsite.com.com/myscript.php?blocks=Block 5</p><a href="">Link 1</a>
<p id='blocks'>Block 6<p id='links'>http://www.testsite.com.com/myscript.php?blocks=Block 6</p><a href="http://www.hereisthelink.com/test.asx">Link 1</a>
<p id='blocks'>Block 7<p id='links'>http://www.testsite.com.com/myscript.php?blocks=Block 7</p><a href="">Link 1</a>
以下是搜索元素的PHP源代码:
<?php
function db_connect()
{
define('DB_HOST', 'localhost');
define('DB_USER', 'myusername');
define('DB_PASSWORD', 'mypassword');
define('DB_DATABASE', 'mydbname');
$errmsg_arr = array();
$errflag = false;
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link)
{
die('Failed to connect to server: ' . mysql_error());
}
$db = mysql_select_db(DB_DATABASE);
if(!$db)
{
die("Unable to select database");
}
}
function clean($var)
{
return mysql_real_escape_string(strip_tags($var));
}
$blocks = clean($_GET['blocks']);
if($errflag)
{
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
echo implode('<br />',$errmsg_arr);
}
else
{
$insert = array();
if(isset($_GET['blocks']))
{
$insert[] = 'blocks = \'' . clean($_GET['blocks']) .'\'';
}
mysql_close();
if(!$blocks)
{
$qrytable1="SELECT id, blocks, links FROM mydb_info";
$result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
while ($row = mysql_fetch_array($result1))
{
echo "<p id='blocks'>";
echo $row['blocks'];
echo '<a href="' . $row['links'] . '">Link 1</a>';
}
}
}
?>
答案 0 :(得分:1)
现在我处理获取网址的方式将是一种更快的方法。如果你知道你的HTML文件中说Block 6应该匹配“http://www.hereisthelink.com/test.asx”我会做这样的事情。
<div id="image6" class="blocksList div_6_1"
data-url="http://www.hereisthelink.com/test.asx">Block 6</div>
我认为这是一个开始 - 选中的框将具有“yellowbg”类,这里我从该框中提取文本并将其置于警报中。
if (event.keyCode == 13)
{
alert("enter tested");
var textFromBlock = $('.yellowbg').html();
var urlFromBlock = $('.yellowbg').attr('data-url');
alert("text from box I selected: " + textFromBlock);
alert("url I wanted: " + urlFromBlock);
}
如果这是一个选项,请执行此操作。原因是jQuery正在搜索整个dom以找到.yellowbg所在的位置。然后你将在整个DOM中搜索文本“Block 6”,然后让它找到下一个标签,然后从那里读取HTML?这是你无缘无故的额外工作。只需将属于Block 6的url放在Block 6中的特殊属性中就可以读取它了。
如果你必须去找到第6块的
,那么就这样做。
<p id='Block 6'>Block 6</p><p id='links'>http://www.testsite.com.com/myscript.php?blocks=Block 6</p><a href="http://www.hereisthelink.com/test.asx">Link 1</a>
你错过了一个结束</p>
并且你有一堆具有相同ID,更改ID以匹配Block 1,2,3,4等。
if (event.keyCode == 13)
{
alert("enter tested");
var textFromBlock = $('.yellowbg').html();
var urlFromBlock = $("p[id='" + textFromBlock + "']").next('a').attr("href");
alert("text from box I selected: " + textFromBlock);
alert("url I wanted: " + urlFromBlock);
}