Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php DOMDocument:在特定id中选择特定类名?_Php_Domdocument - Fatal编程技术网

php DOMDocument:在特定id中选择特定类名?

php DOMDocument:在特定id中选择特定类名?,php,domdocument,Php,Domdocument,嘿,伙计们, DOMDocument类的两个问题 我正在使用jQuery执行此操作: $('#wpf-wrapper .wpf-table .wpf-input').addClass('input-field'); 我想对DOMDocument类也这样做!如何做到这一点?如何选择此选项 第二个问题是:为什么这不起作用 $dom = new SmartDOMDocument(); $dom->loadHTML($shortcode); $xpath = new DOMXPath($dom)

嘿,伙计们, DOMDocument类的两个问题

我正在使用jQuery执行此操作:

$('#wpf-wrapper .wpf-table .wpf-input').addClass('input-field');
我想对DOMDocument类也这样做!如何做到这一点?如何选择此选项

第二个问题是:为什么这不起作用

$dom = new SmartDOMDocument();
$dom->loadHTML($shortcode);
$xpath = new DOMXPath($dom);

$qr = $dom->getElementById('quick-reply-submit');
//$qr->setAttribute('class', 'btn small width480');
//ERROR: Call to a member function on a non-object...??
谢谢你的帮助

您可以使用搜索,然后迭代查询结果,将必要的值添加到
属性(如果该属性尚未存在):

// This is the equivalent of '#wpf-wrapper .wpf-table .wpf-input'
$expr = '//*[@id="wpf-wrapper"]//*[@class="wpf-table"]//*[@class="wpf-input"]';

$result = $xpath->query($expr);
for($i = 0; $i < $result->length; ++$i) {
    // Get the classes of each matched element
    $classes = explode(' ', $result->item($i)->getAttribute('class'));

    // Add "input-field" if it's not in there already
    if(!in_array('input-field', $classes)) {
        $classes[] = 'input-field';
    }

    // Set the class attribute to the new value
    $result->item($i)->setAttribute('class', implode(' ', $classes));
}
//这相当于“#wpf wrapper.wpf table.wpf input”
$expr='/*[@id=“wpf wrapper”]/*[@class=“wpf table”]//*[@class=“wpf input”];
$result=$xpath->query($expr);
对于($i=0;$i<$result->length;++$i){
//获取每个匹配元素的类
$classes=explode(“”,$result->item($i)->getAttribute('class');
//如果“输入字段”不在其中,则添加“输入字段”
if(!in_数组('input-field',$classes)){
$classes[]=“输入字段”;
}
//将class属性设置为新值
$result->item($i)->setAttribute('class',内爆('''class');
}

您的“第二个问题”代码不起作用,因为没有id为
quick reply submit
的元素——没有其他可能性。

这是一个非常有用的类,它扩展了DOMDocument类以获得更好的UTF-8支持,等等。是否在中提供带有指向该库的链接的答案?放在那里没什么不好的。谢谢你。。。最后一个问题。如何在一系列选择器中选择标记名,例如<代码>$expr='/*[@id=“wpf包装器”]/*[@class=“wpf表格”]//*[@tagname=“a”]”所以我想选择wpf表内部的所有锚定?@mathiregister:那一个是
//*[@class=“wpf table”]///a
。查看XPath语法教程了解更多信息,有很多种可能性。这里有一个: