例如
我要匹配出來一個HTML標籤的class 或者 id 是什麼。。(有ID拿ID,沒有就拿CLASS)
最好也能匹配出來到底是div還是span還是其它
回複討論(解決方案)
提供一個jQuery的解決方式
點我試試
點我試試
剛剛沒注意將其寫入到原始碼中,使得格式錯亂,看如下:
function getTagInfo(e){var str='';if(jQuery(e).attr('id')){str += 'id:'+jQuery(e).attr('id');}else if(jQuery(e).attr('class')){str += 'class:'+jQuery(e).attr('class');}else{str += '既沒設定id也沒設定class屬性';}str += '\ntagName:'+jQuery(e).get(0).tagName;alert(str);}
點我點我
$html = '';$dom = new DOMDocument();@$dom->loadHTML($html);$x = new DOMXPath($dom); foreach($x->query("//div") as $node)//如果要span的話,//div 換成 //span { echo $node->getAttribute("id");echo '
';echo $node->getAttribute("class");}
我也沒看清楚,原來span或者div是不一定的,那就換 //* 然後 nodeName 擷取HTML標籤名。
loadHTML($html);$x = new DOMXPath($dom); foreach($x->query("//*") as $node) { echo $node->getAttribute("id"); //myidecho '
';echo $node->getAttribute("class"); //myclassecho '
';echo $node->nodeName; // div} ?>
[/code]
用PHPQUERY 可以實現嗎
[/code]
用PHPQUERY 可以實現嗎
sorry!PHPQUERY我沒接觸過