我正在尝试在保留图片的同时从我的链接中删除 Title
文字。
<tr id="group0">
<td colspan="100" nowrap="" class="ms-gb">
<a href="javascript:" onclick="javascript:ExpCollGroup('28-1_', 'img_28-1_',event, false);return false;">
<img src="/_layouts/15/images/minus.gif" border="0" alt="collapse" id="img_28-1_">
Title
</a>
: How do I access SharePoint settings?
</td>
</tr>
我面临的问题是来自SharePoint网站,我对之前设置的HTML没有太多控制权,我只能在使用JQuery生成页面后对其进行修改。此代码有大约20个不同的实例在页面上重复,但每个实例都有<a>
标记中引用的img id以及必须保持唯一的<img>
标记,所以我的尝试做一些事情:
$('.ms-gb a').contents().filter(function() {
return this.nodeType == 3;
}).text();
返回“标题”的所有20个实例。我想可能使用Jquery来抓取<a>
标签内的html,然后使用字符串拼接来重新调整最后11个字符,但到目前为止似乎没有成功。
答案 0 :(得分:1)
要删除它,您可以获取contents()
,然后在生成的集合中的最后一项上调用remove()
。试试这个:
var contents = $('.ms-gb a').contents();
contents[contents.length -1].remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr id="group0">
<td colspan="100" nowrap="" class="ms-gb">
<a href="javascript:" onclick="javascript:ExpCollGroup('28-1_', 'img_28-1_',event, false);return false;">
<img src="/_layouts/15/images/minus.gif" border="0" alt="collapse" id="img_28-1_"> Title
</a>
: How do I access SharePoint settings?
</td>
</tr>
</table>
另外,作为旁注,将JS代码放在href
或on*
属性中并不是一个好习惯。您应该考虑使用不显眼的事件处理程序。我知道你正在使用Sharepoint,所以根据页面的构建方式,你的双手可能会受到限制。
不幸的是,由于.ms-gb的重复,整个文件试图用内容选择它给了我一套50
在这种情况下,您可以单独遍历所有.ms-gb a
元素,如下所示:
$('.ms-gb a').each(function() {
var contents = $(this).contents();
contents[contents.length -1].remove();
});
答案 1 :(得分:0)
尝试:
//get and declare seralizer, then deserialize
List<Item> items = serializer.Deserialize<Item>().ToList();
//find a specific item by name
string searchName = "UtensilSpoon";
var mySearchItem = items.FirstOrDefault(i => i.Name == searchName); //null if not found
//get all the items from a given primary provider
string primaryProvider = "Farmer";
var mySearchItems = items.Where(i => i.Primaryprovider == primaryProvider);
//get all the items in a given category
string category = "Grocery";
var itemsInCategory = items.Where(i => i.Categories.Contains(category));
//get the origin object for items in a given state
string state = "Texas";
var texasItems = items.Where(i => i.Origin.State == state);
//print out the item name, state, and year for texas items only
string state = "Texas";
foreach (var item in items.Where(i => i.Origin.State == state)) //(var item in texasItems) would also work
{
Console.WriteLine(item.Name);
Console.WriteLine(item.Origin.State);
Console.WriteLine(item.Origin.Year);
}
运行此应该会为您留下带有图像的链接,除去任何其他元素。
答案 2 :(得分:0)
$('.ms-gb a').each(function(){
this.lastChild.remove();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tr id="group0">
<td colspan="100" nowrap="" class="ms-gb">
<a href="javascript:" onclick="javascript:ExpCollGroup('28-1_', 'img_28-1_',event, false);return false;">
<img src="/_layouts/15/images/minus.gif" border="0" alt="collapse" id="img_28-1_">
Title
</a>
: How do I access SharePoint settings?
</td>
</tr>
<tr id="group1">
<td colspan="100" nowrap="" class="ms-gb">
<a href="javascript:" onclick="javascript:ExpCollGroup('28-1_', 'img_28-1_',event, false);return false;">
<img src="/_layouts/15/images/minus.gif" border="0" alt="collapse" id="img_28-1_">
Title
</a>
: How do I access SharePoint settings?
</td>
</tr>
</table>
你可以通过$('.ms-gb a')
获取所有元素,并通过调用lastChild.remove()删除该lastChild;它将删除该文本。