php html dom解析器中的内联样式设置问题
嗨,我正在一个天气网站上做一个屏幕截图,该网站的div中有内联样式,没有类或id。下面是他们的代码:php html dom解析器中的内联样式设置问题,php,css,Php,Css,嗨,我正在一个天气网站上做一个屏幕截图,该网站的div中有内联样式,没有类或id。下面是他们的代码: <div class="TodaysForecastContainer"> <div class="TodaysForecastContainerInner"> <div style="font-size:12px;"><u>This morning</
<div class="TodaysForecastContainer">
<div class="TodaysForecastContainerInner">
<div style="font-size:12px;"><u>This morning</u></div>
<div style="position:absolute;top:17px;left:3px;">
<a href="forecastPublicExtended.asp#Period0" target="_blank">
<img src="./images/wimages/b_cloudy.gif" height="50px" width="50px" alt="weather image">
</a> </div>
<div style="position:absolute; top:25px; left:57px; text-align:left; height:47px; width:90px;">
Sunny Breaks </div>
</div>
<div class="TodaysForecastContainerInner">
<div style="font-size:12px;"><u>This afternoon</u></div>
<div style="position:absolute;top:17px;left:3px;">
<a href="forecastPublicExtended.asp#Period0" target="_blank">
<img src="./images/wimages/b_pcloudy.gif" height="50px" width="50px" alt="weather image">
</a> </div>
<div style="position:absolute; top:25px; left:57px; text-align:left; height:47px; width:90px;">
Mix of Sun and Cloud </div>
</div>
今天早上
晴天休息
今天下午
太阳和云的混合
问题是绝对位置内联样式,它们没有类或id,我希望我可以添加一个类名并删除带有“This morning”的div上的内联样式,包含图像的div,还可以删除带有说明的链接和div(例如Sunny Breaks)也改变了今天所有的预测容器,因为它有大约4个预测。使其类似于:
<div class="day>This morning</div><div class="thumbnail"><img src="sample.jpg"></div><div class="description">Sunny Breaks</div>
在客户机上执行此操作比在服务器上更容易
此jQuery+Javascript将清除内联样式,并为每个样式应用类名:
$(document).ready(function() {
var target = $('.TodaysForecastContainerInner div')
for(var x=0;x< target.length;x++) {
target.eq(x).attr('style','');
target.eq(x).addClass("A_"+x)
}
})
$(文档).ready(函数(){
var target=$('.TodaysForCastContainerIner div'
对于(var x=0;x
结果:
<div class="TodaysForecastContainerInner">
<div style="" class="A_0"><u>This morning</u></div>
<div style="" class="A_1">
<a target="_blank" href="forecastPublicExtended.asp#Period0">
<img height="50px" width="50px" alt="weather image" src="./images/wimages/b_cloudy.gif">
</a> </div>
<div style="" class="A_2">
Sunny Breaks </div>
</div>
<div class="TodaysForecastContainer">
<div class="TodaysForecastContainerInner">
<div class="day">This morning</div>
<div class="thumbnail"><img src="./images/wimages/b_cloudy.gif"></div>
<div class="description">
Sunny Breaks
</div>
</div>
<div class="TodaysForecastContainerInner">
<div class="day">This afternoon</div>
<div class="thumbnail"><img src="./images/wimages/b_pcloudy.gif"></div>
<div class="description">
Mix of Sun and Cloud
</div>
</div>
今天早上
晴天休息
您可以使用样式表使其外观符合您的要求。查看库。它可以使用PHP进行类似jQuery的操作。此代码基本上完成了您正在尝试执行的操作:
<?php
include 'phpQuery-onefile.php';
$text = <<<EOF
<div class="TodaysForecastContainer">
<div class="TodaysForecastContainerInner">
<div style="font-size:12px;"><u>This morning</u></div>
<div style="position:absolute;top:17px;left:3px;">
<a href="forecastPublicExtended.asp#Period0" target="_blank">
<img src="./images/wimages/b_cloudy.gif" height="50px" width="50px" alt="weather image">
</a>
</div>
<div style="position:absolute; top:25px; left:57px; text-align:left; height:47px; width:90px;">
Sunny Breaks
</div>
</div>
<div class="TodaysForecastContainerInner">
<div style="font-size:12px;"><u>This afternoon</u></div>
<div style="position:absolute;top:17px;left:3px;">
<a href="forecastPublicExtended.asp#Period0" target="_blank">
<img src="./images/wimages/b_pcloudy.gif" height="50px" width="50px" alt="weather image">
</a>
</div>
<div style="position:absolute; top:25px; left:57px; text-align:left; height:47px; width:90px;">
Mix of Sun and Cloud
</div>
</div>
EOF;
$doc = phpQuery::newDocumentHTML( $text );
$containers = pq('.TodaysForecastContainerInner', $doc);
foreach( $containers as $container ) {
$div = pq('div', $container);
$div->eq(0)->removeAttr('style')->addClass('day')->html( pq( 'u', $div->eq(0) )->html() );
$div->eq(1)->removeAttr('style')->addClass('thumbnail')->html( pq( 'img', $div->eq(1))->removeAttr('height')->removeAttr('width')->removeAttr('alt') );
$div->eq(2)->removeAttr('style')->addClass('description');
}
print $doc;
谢谢你的回复不幸的是我没有得到你的回复,这里是我的测试点。你能告诉我你是怎么做的吗这里是我的php//find all foreach($html->find('.TodaysForecastContainerInner div')as$e)echo$e->innertext
';另外,我在$(document).ready(function(){var target=$)上出错了(我不知道怎么了这是源代码,你可以查看我放在上面的php谢谢你的评论我在$text=Hrm上出现语法错误了…我只是通过粘贴和复制再次尝试,它对我有效。不过没什么大不了的。“$text=
<div class="TodaysForecastContainer">
<div class="TodaysForecastContainerInner">
<div class="day">This morning</div>
<div class="thumbnail"><img src="./images/wimages/b_cloudy.gif"></div>
<div class="description">
Sunny Breaks
</div>
</div>
<div class="TodaysForecastContainerInner">
<div class="day">This afternoon</div>
<div class="thumbnail"><img src="./images/wimages/b_pcloudy.gif"></div>
<div class="description">
Mix of Sun and Cloud
</div>
</div>