将每个可见的src更改为它的数据属性

时间:2017-04-28 15:42:19

标签: javascript jquery

for($i = 0; $i< 5; $i++){
curl_setopt($ch, CURLOPT_URL, 'url/details.php?med_id='.$i);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$answer = curl_exec($ch);
//echo $answer;
if (curl_error($ch)) {
    echo curl_error($ch);
}

$html = str_get_html($answer);

    $table = $html->find('table', 3);

    /*
*$table->find('td',17)->plaintext => could be "Comercializado" or "Não comercializado", so if is  "Não comercializado" it goes to the next iteration.
I already output the text "$table->find('td',17)->plaintext" to see if appeaars and looks ok.
*/
    if (strpos($table->find('td',17)->plaintext, 'Não comercializado') !== false) {
        continue;
    }


    echo "<tr>";
    echo "<td>".$table->find('td',17)."</td>";
    echo "<td>".$table->find('td',14)."</td>";
    echo " <td>".$table->find('td',22)."</td>";
     echo "<td>".$table->find('td',25)."</td>";
    echo "</tr>";

}

 echo "</table>";

如何修复不工作的部分?

2 个答案:

答案 0 :(得分:0)

问题是因为您已经定义了名为bild的变量,而不是data

另请注意,通过向attr()提供函数,您可以使逻辑更简洁:

$("[data-musterfull]:visible").attr('src', function() {
  return $(this).data('musterfull');
});

答案 1 :(得分:0)

应该是

$("[data-musterfull]:visible").each(function(){
    var bild = $(this).data('musterfull');
    $(this).attr('src', bild); 
});

你应该通过&#39; bild&#39;变量到attr(&#39; src&#39;)函数而不是&#39;数据&#39;。