这是问题所在:
<h2><b>Progress: </b> <font color="87edff">3 / 5</font> <b>Clicks</b></h2>
我想在PHP或Javascript中创建一个脚本来检查第一个数字(在这种情况下是3),如果它大于某个数字(例如5),要做一些诸如显示隐藏文本,打开一个链接等等。
有谁知道这可以(或者如果不能)这样做?我想过使用POST + GET变量,但没有成功。
答案 0 :(得分:1)
使用正则表达式获取3/5,然后在/
之后拆分,然后在数组中进行比较。
你也可以作为另一条路线,命名包含3/5的元素,并使用jquery获取该值。然后你会这样做,在/
上拆分字符串并进行比较。
POST / GET仅在提交表单或向serer上的脚本发送数据时可用。这些主要用于textarea
和输入
答案 1 :(得分:0)
如果你已经在使用jquery,一些花哨的jquery选择器会这样做:
findThings = function(){
// select the first <b> elements that contain "Clicks", AND
// has a sibling before it that is a <font>, AND
// has a sibling THAT has both an <h2> as a parent, and contains the text "Progress".
var firstnumber = -1; // some number that doesn't make sense
var secondnumber = -1;
$('h2 > b:contains("Progress:") + font + b:contains("Clicks"):first').each(
function(){
//alert("Element is:",this);
//alert("Element contains:" + $(this).text());
// "this" is the <b> that contains "Clicks". Find the first sibling that is a <font> and grab the text from it.
text = $(this).siblings("font:first").text();
//alert(text);
numbers = text.split("/"); //split the text by "/" character.
if (numbers.length == 2){ // make sure there are two numbers!
firstnumber = numbers[0];
secondnumber = numbers[1];
}
}
);
alert("First number:" + firstnumber);
alert("Second number:" + secondnumber);
var whatever = 5;
if(firstnumber >= whatever){
alert("YOU WIN!");
}else{
alert("You Lose!");
}
}
但这只适用于代码放在同一网站上的情况。如果它在同一个站点上,那么这是一个可怕的想法,因为你可以在元素上粘贴一个id并通过id访问值。尽管如此,还是展示了很酷的选择器。