xlink的jQuery选择器

时间:2014-09-24 08:47:14

标签: jquery css

$('a[xlink\\:href=#coastline]').attr('class','grey');
$('a[xlink\\:href=#onshore]').attr('class','blue-light');

这是我目前必须选择xlink为#coastline的每个项目并将其变为灰色并将#onshore转为blue-light

我如何更改上述内容以选择任何a[xlink\\:href]并为其提供课程?

我尝试了$('a[xlink:href]').attr('class', 'yellow');,但这并没有给他们一类yellow

5 个答案:

答案 0 :(得分:1)

xlink可以在xml标记中找到,并且因为它们位于另一个名称空间中而难以选择。

您可以尝试遍历所有元素并修改DOM元素className

var $elements = $('a[xlink\\:href]');
$elements.each(function(index, element) {
    element.className = 'my-new-class';
});

UPDATE:当前选择器应返回null,因为它是命名空间的问题。 我们可以使用特殊选择器a[*|xlink] Stack Overflow post reference

来修复此命名空间

我们知道它是SVG,所以要更改SVG对象类,我们必须对它们使用attr函数。 如果你只想选择SVG元素中的所有链接,我会得到类似的东西:

var $svg = $('svg');
var $elements = $('a[*|xlink]', $svg);  // Select all a xlink attributes inside svg object


$elements.each(function(index, element) {
    $(element).attr('class', 'my-new-class');  // force class attribute
});

JSFiddle:http://jsfiddle.net/nbfdydzd/

答案 1 :(得分:1)

您需要转义特殊字符

$('a[xlink\\:href]').attr('class', 'yellow');

答案 2 :(得分:1)

从你的评论中我认为你有点像

 <a xlink:href="http://www.google.com" target="_blank">
     <text x="0" y="15" fill="red">Link text here</text>
 </a>

因此,要更改它的颜色,您可以尝试更改它的fill属性,如

 $('a[xlink\\:href=#coastline]').attr('fill','#ff00ff');

答案 3 :(得分:1)

href将匹配html xlink:href和svg :not([href]),然后使用href排除html //document.querySelectorAll('[*|href]:not([href])'); $('[*|href]:not([href])');

JSONObject object = new JSONArray(s.toString());
JSONArray arr1 = new JSONArray(object.getString("data").toString()).getJSONArray(0);
for (int i = 0; i < arr1.length(); i++) {
 JSONObject jsonObject = arr1.getJSONObject(i);
 HashMap<String, String> data = new HashMap<String, String>();
 data.put("id", jsonObject.getString("id"));
 data.put("date_inspected_details", jsonObject.getString("date_inspected_details"));
 addressaray.add(data);
    }
    adapter = new PropertiesEvaluatedFragmentAdapter(getActivity(), addressaray, getActivity().getSupportFragmentManager());
  listView.setAdapter(adapter);
  adapter.notifyDataSetChanged();
}

 @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    String item = parent.getItemAtPosition(position).toString();
 if(addressaray.size() > 0){
                sortListView(item);
                adapter.notifyDataSetChanged();
            }else{
                Toast.makeText(getActivity(), "No Record found.", Toast.LENGTH_SHORT).show();
            }

}

  private void sortListView(String option) {
        switch (option) {
            case "Date":

Collections.sort(addressaray, new Comparator<HashMap<String, String>>() {
                    final static String COMPARE_KEY = "date_inspected_details";

                    @Override
                    public int compare(HashMap<String, String> lhs,
                                       HashMap<String, String> rhs) {
                        String Date1 = lhs.get(COMPARE_KEY);
                        String Date2 = rhs.get(COMPARE_KEY);
                        Log.e("Date1", Date1);
                        Log.e("Date2", Date2);
                        // Do your comparison logic here and retrn accordingly.
                        return Date1.compareTo(Date2);
                    }
                });
  break;
}
}JSONObject object = new JSONArray(s.toString());
JSONArray arr1 = new JSONArray(object.getString("data").toString()).getJSONArray(0);
for (int i = 0; i < arr1.length(); i++) {
 JSONObject jsonObject = arr1.getJSONObject(i);
 HashMap<String, String> data = new HashMap<String, String>();
 data.put("id", jsonObject.getString("id"));
 data.put("date_inspected_details", jsonObject.getString("date_inspected_details"));
 addressaray.add(data);
    }
    adapter = new PropertiesEvaluatedFragmentAdapter(getActivity(), addressaray, getActivity().getSupportFragmentManager());
  listView.setAdapter(adapter);
  adapter.notifyDataSetChanged();
}

 @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    String item = parent.getItemAtPosition(position).toString();
 if(addressaray.size() > 0){
                sortListView(item);
                adapter.notifyDataSetChanged();
            }else{
                Toast.makeText(getActivity(), "No Record found.", Toast.LENGTH_SHORT).show();
            }

}

  private void sortListView(String option) {
        switch (option) {
            case "Date":

Collections.sort(addressaray, new Comparator<HashMap<String, String>>() {
                    final static String COMPARE_KEY = "date_inspected_details";

                    @Override
                    public int compare(HashMap<String, String> lhs,
                                       HashMap<String, String> rhs) {
                        String Date1 = lhs.get(COMPARE_KEY);
                        String Date2 = rhs.get(COMPARE_KEY);
                        Log.e("Date1", Date1);
                        Log.e("Date2", Date2);
                        // Do your comparison logic here and retrn accordingly.
                        return Date1.compareTo(Date2);
                    }
                });
  break;
}
}

在chrome中测试

答案 4 :(得分:0)

也许这就是你想要的Demo

$('a').each(function(i){
    if($(this).attr('href') == "#coastline") $(this).addClass('gray');
    if($(this).attr('href') == "#onshore") $(this).addClass('green');
    if($(this).attr('href') == "") $(this).addClass('yello');
});